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 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / wc-cart-functions.php
woocommerce / includes Last commit date
abstracts 6 days ago admin 6 days ago blocks 1 month ago cli 2 weeks ago customizer 6 months ago data-stores 6 days ago emails 2 weeks ago export 2 weeks ago gateways 2 weeks ago import 1 week ago integrations 1 month ago interfaces 6 months ago legacy 1 month ago libraries 2 weeks ago log-handlers 1 year ago payment-tokens 6 years ago product-usage 1 year ago queue 6 months ago react-admin 1 week ago rest-api 4 days ago shipping 5 months ago shortcodes 2 weeks ago theme-support 2 years ago tracks 2 weeks ago traits 5 years ago walkers 5 years ago wccom-site 2 months ago widgets 1 month ago class-wc-ajax.php 4 days ago class-wc-auth.php 2 years ago class-wc-autoloader.php 11 months ago class-wc-background-emailer.php 4 months ago class-wc-background-updater.php 6 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 2 weeks ago class-wc-breadcrumb.php 6 months ago class-wc-cache-helper.php 2 months ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 weeks ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 month ago class-wc-checkout.php 1 month ago class-wc-cli.php 2 weeks ago class-wc-comments.php 1 week ago class-wc-countries.php 4 months ago class-wc-coupon.php 2 weeks ago class-wc-customer-download-log.php 6 years ago class-wc-customer-download.php 2 years ago class-wc-customer.php 1 month ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 5 months ago class-wc-discounts.php 2 weeks ago class-wc-download-handler.php 2 weeks ago class-wc-emails.php 1 month ago class-wc-embed.php 3 weeks ago class-wc-form-handler.php 2 weeks ago class-wc-frontend-scripts.php 1 month ago class-wc-geo-ip.php 11 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 2 weeks ago class-wc-https.php 3 years ago class-wc-install.php 1 week ago class-wc-integrations.php 6 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 6 months ago class-wc-meta-data.php 2 months ago class-wc-order-factory.php 4 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 2 weeks ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 weeks ago class-wc-order-item-shipping.php 7 months ago class-wc-order-item-tax.php 2 months ago class-wc-order-item.php 1 month ago class-wc-order-query.php 2 months ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 month ago class-wc-payment-gateways.php 2 weeks ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 month ago class-wc-post-types.php 2 weeks ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 5 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 6 months ago class-wc-product-download.php 1 month ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 5 months ago class-wc-product-grouped.php 1 month ago class-wc-product-query.php 6 months ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 week ago class-wc-product-variation.php 2 weeks ago class-wc-query.php 1 month ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 2 weeks ago class-wc-regenerate-images.php 2 weeks ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 4 days ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 3 weeks ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 2 weeks ago class-wc-shipping.php 1 month ago class-wc-shortcodes.php 2 weeks ago class-wc-structured-data.php 2 weeks ago class-wc-tax.php 1 month ago class-wc-template-loader.php 9 months ago class-wc-tracker.php 1 month ago class-wc-validation.php 1 month ago class-wc-webhook.php 2 weeks ago class-woocommerce.php 3 days ago wc-account-functions.php 2 months ago wc-attribute-functions.php 1 month ago wc-brands-functions.php 1 year ago wc-cart-functions.php 2 weeks ago wc-conditional-functions.php 2 weeks ago wc-core-functions.php 2 weeks ago wc-coupon-functions.php 7 months ago wc-deprecated-functions.php 2 weeks ago wc-formatting-functions.php 2 weeks ago wc-interactivity-api-functions.php 4 months ago wc-notice-functions.php 7 months ago wc-order-functions.php 2 weeks ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 3 weeks ago wc-page-functions.php 1 month ago wc-product-functions.php 2 weeks ago wc-rest-functions.php 9 months ago wc-stock-functions.php 1 month ago wc-template-functions.php 1 week ago wc-template-hooks.php 1 year ago wc-term-functions.php 2 weeks ago wc-update-functions.php 1 week ago wc-user-functions.php 1 month ago wc-webhook-functions.php 2 months ago wc-widget-functions.php 6 years ago
wc-cart-functions.php
629 lines
1 <?php
2 /**
3 * WooCommerce Cart Functions
4 *
5 * Functions for cart specific things.
6 *
7 * @package WooCommerce\Functions
8 * @version 2.5.0
9 */
10
11 use Automattic\Jetpack\Constants;
12 use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
13 use Automattic\WooCommerce\Enums\OrderStatus;
14 use Automattic\WooCommerce\Enums\ProductType;
15 use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Prevent password protected products being added to the cart.
21 *
22 * @param bool $passed Validation.
23 * @param int $product_id Product ID.
24 * @return bool
25 */
26 function wc_protected_product_add_to_cart( $passed, $product_id ) {
27 if ( post_password_required( $product_id ) ) {
28 $passed = false;
29 wc_add_notice( __( 'This product is protected and cannot be purchased.', 'woocommerce' ), 'error' );
30 }
31 return $passed;
32 }
33 add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );
34
35 /**
36 * Clears the cart session when called.
37 */
38 function wc_empty_cart() {
39 if ( ! isset( WC()->cart ) || '' === WC()->cart ) {
40 WC()->cart = new WC_Cart();
41 }
42 WC()->cart->empty_cart( false );
43 }
44
45 /**
46 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
47 *
48 * Do not use for redirects, use {@see wp_get_referer()} instead.
49 *
50 * @since 2.6.1
51 * @return string|false Referer URL on success, false on failure.
52 */
53 function wc_get_raw_referer() {
54 if ( function_exists( 'wp_get_raw_referer' ) ) {
55 return wp_get_raw_referer();
56 }
57
58 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { // WPCS: input var ok, CSRF ok.
59 return wp_unslash( $_REQUEST['_wp_http_referer'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
60 } elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { // WPCS: input var ok, CSRF ok.
61 return wp_unslash( $_SERVER['HTTP_REFERER'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
62 }
63
64 return false;
65 }
66
67 /**
68 * Add to cart messages.
69 *
70 * @param int|array $products Product ID list or single product ID.
71 * @param bool $show_qty Should quantities be shown? Added in 2.6.0.
72 * @param bool $return Return message rather than add it.
73 *
74 * @return mixed
75 */
76 function wc_add_to_cart_message( $products, $show_qty = false, $return = false ) {
77 $titles = array();
78 $count = 0;
79
80 if ( ! is_array( $products ) ) {
81 $products = array( $products => 1 );
82 $show_qty = false;
83 }
84
85 if ( ! $show_qty ) {
86 $products = array_fill_keys( array_keys( $products ), 1 );
87 }
88
89 $product_id = null;
90 foreach ( $products as $product_id => $qty ) {
91 /**
92 * Filters the quantity HTML for the add to cart message.
93 *
94 * @since 2.6.1
95 * @param string $qty_html The quantity HTML.
96 * @param int $product_id The product ID.
97 */
98 $title = apply_filters( 'woocommerce_add_to_cart_qty_html', ( 1 !== $qty ? wc_stock_amount( $qty ) . ' &times; ' : '' ), $product_id );
99
100 /**
101 * Filters the item name in quotes for the add to cart message.
102 *
103 * @since 2.6.1
104 * @param string $item_name_in_quotes The item name in quotes.
105 * @param int $product_id The product ID.
106 */
107 $title .= apply_filters(
108 'woocommerce_add_to_cart_item_name_in_quotes',
109 sprintf(
110 /* translators: %s: product name */
111 _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ),
112 wp_strip_all_tags( get_the_title( $product_id ) )
113 ),
114 $product_id
115 );
116
117 $titles[] = $title;
118 $count += $qty;
119 }
120
121 $titles = array_filter( $titles );
122 /* translators: %s: product name */
123 $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
124
125 // Output success messages.
126 $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
127 if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
128 $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
129 $message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_attr( $wp_button_class ), esc_html__( 'Continue shopping', 'woocommerce' ) );
130 } elseif ( ! CartCheckoutUtils::has_cart_page() ) {
131 $message = sprintf( '%s', esc_html( $added_text ) );
132 } else {
133 $message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', esc_html( $added_text ), esc_url( wc_get_cart_url() ), esc_attr( $wp_button_class ), esc_html__( 'View cart', 'woocommerce' ) );
134 }
135
136 if ( has_filter( 'wc_add_to_cart_message' ) ) {
137 wc_deprecated_function( 'The wc_add_to_cart_message filter', '3.0', 'wc_add_to_cart_message_html' );
138 $message = apply_filters( 'wc_add_to_cart_message', $message, $product_id );
139 }
140
141 $message = apply_filters( 'wc_add_to_cart_message_html', $message, $products, $show_qty );
142
143 if ( $return ) {
144 return $message;
145 } else {
146 wc_add_notice( $message, apply_filters( 'woocommerce_add_to_cart_notice_type', 'success' ) );
147 }
148 }
149
150 /**
151 * Comma separate a list of item names, and replace final comma with 'and'.
152 *
153 * @param array $items Cart items.
154 * @return string
155 */
156 function wc_format_list_of_items( $items ) {
157 $item_string = '';
158
159 foreach ( $items as $key => $item ) {
160 $item_string .= $item;
161
162 if ( count( $items ) === $key + 2 ) {
163 $item_string .= ' ' . __( 'and', 'woocommerce' ) . ' ';
164 } elseif ( count( $items ) !== $key + 1 ) {
165 $item_string .= ', ';
166 }
167 }
168
169 return $item_string;
170 }
171
172 /**
173 * Clear cart after payment.
174 */
175 function wc_clear_cart_after_payment() {
176 global $wp;
177
178 $should_clear_cart_after_payment = false;
179 $after_payment = false;
180 $order = null;
181
182 // If the order has been received, clear the cart.
183 if ( ! empty( $wp->query_vars['order-received'] ) ) {
184
185 $order_id = absint( $wp->query_vars['order-received'] );
186 $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok.
187
188 if ( $order_id > 0 ) {
189 $order = wc_get_order( $order_id );
190
191 if ( $order instanceof WC_Order && hash_equals( $order->get_order_key(), $order_key ) ) {
192 $should_clear_cart_after_payment = true;
193 $after_payment = true;
194 }
195 }
196 }
197
198 // If the order is awaiting payment, and we haven't already decided to clear the cart, check the order status.
199 if ( is_object( WC()->session ) && WC()->session->order_awaiting_payment > 0 && ! $should_clear_cart_after_payment ) {
200 $order = wc_get_order( WC()->session->order_awaiting_payment );
201
202 if ( $order instanceof WC_Order && $order->get_id() > 0 ) {
203 // If the order status is neither pending, failed, nor cancelled, the order must have gone through.
204 $should_clear_cart_after_payment = ! $order->has_status( array( OrderStatus::FAILED, OrderStatus::PENDING, OrderStatus::CANCELLED ) );
205 $after_payment = true;
206 }
207 }
208
209 // If it doesn't look like a payment happened, bail early.
210 if ( ! $after_payment ) {
211 return;
212 }
213
214 // If the order is different from the cart, don't clear the cart. This can happen if the user has multiple tabs open and completes a different order than the one in the cart.
215 if ( $should_clear_cart_after_payment && $order instanceof WC_Order && ! WC()->cart->is_empty() ) {
216 $should_clear_cart_after_payment = $order->has_cart_hash( WC()->cart->get_cart_hash() );
217 }
218
219 /**
220 * Determine whether the cart should be cleared after payment.
221 *
222 * @since 9.3.0
223 * @param bool $should_clear_cart_after_payment Whether the cart should be cleared after payment.
224 */
225 $should_clear_cart_after_payment = apply_filters( 'woocommerce_should_clear_cart_after_payment', $should_clear_cart_after_payment );
226
227 if ( $should_clear_cart_after_payment ) {
228 WC()->cart->empty_cart();
229 }
230 }
231 add_action( 'template_redirect', 'wc_clear_cart_after_payment', 20 );
232
233 /**
234 * Get the subtotal.
235 */
236 function wc_cart_totals_subtotal_html() {
237 echo WC()->cart->get_cart_subtotal(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
238 }
239
240 /**
241 * Get shipping methods.
242 */
243 function wc_cart_totals_shipping_html() {
244 $packages = WC()->shipping()->get_packages();
245 $first = true;
246
247 foreach ( $packages as $i => $package ) {
248 $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
249 $product_names = array();
250
251 if ( count( $packages ) > 1 ) {
252 foreach ( $package['contents'] as $item_id => $values ) {
253 $product_names[ $item_id ] = $values['data']->get_name() . ' &times;' . $values['quantity'];
254 }
255 $product_names = apply_filters( 'woocommerce_shipping_package_details_array', $product_names, $package );
256 }
257
258 wc_get_template(
259 'cart/cart-shipping.php',
260 array(
261 'package' => $package,
262 'available_methods' => $package['rates'],
263 'show_package_details' => count( $packages ) > 1,
264 'show_shipping_calculator' => is_cart() && apply_filters( 'woocommerce_shipping_show_shipping_calculator', $first, $i, $package ),
265 'package_details' => implode( ', ', $product_names ),
266 'package_name' => $package['package_name'],
267 'index' => $i,
268 'chosen_method' => $chosen_method,
269 'formatted_destination' => WC()->countries->get_formatted_address( $package['destination'], ', ' ),
270 'has_calculated_shipping' => WC()->customer->has_calculated_shipping(),
271 )
272 );
273
274 $first = false;
275 }
276 }
277
278 /**
279 * Get taxes total.
280 */
281 function wc_cart_totals_taxes_total_html() {
282 echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
283 }
284
285 /**
286 * Get a coupon label.
287 *
288 * @param string|WC_Coupon $coupon Coupon data or code.
289 * @param bool $echo Echo or return.
290 *
291 * @return string
292 */
293 function wc_cart_totals_coupon_label( $coupon, $echo = true ) {
294 if ( is_string( $coupon ) ) {
295 $coupon = new WC_Coupon( $coupon );
296 }
297
298 /* translators: %s: coupon code */
299 $label = apply_filters( 'woocommerce_cart_totals_coupon_label', sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ), $coupon->get_code() ), $coupon );
300
301 if ( $echo ) {
302 echo $label; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
303 } else {
304 return $label;
305 }
306 }
307
308 /**
309 * Get coupon display HTML.
310 *
311 * @param string|WC_Coupon $coupon Coupon data or code.
312 */
313 function wc_cart_totals_coupon_html( $coupon ) {
314 if ( is_string( $coupon ) ) {
315 $coupon = new WC_Coupon( $coupon );
316 }
317
318 $amount = WC()->cart->get_coupon_discount_amount( $coupon->get_code(), WC()->cart->display_cart_ex_tax );
319 $discount_amount_html = '-' . wc_price( $amount );
320
321 if ( $coupon->get_free_shipping() && empty( $amount ) ) {
322 $discount_amount_html = __( 'Free shipping coupon', 'woocommerce' );
323 }
324
325 $discount_amount_html = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_amount_html, $coupon );
326 // translators: %s: coupon code.
327 $coupon_html = $discount_amount_html . ' <a role="button" aria-label="' . esc_attr( sprintf( __( 'Remove %s coupon', 'woocommerce' ), $coupon->get_code() ) ) . '" href="' . esc_url( add_query_arg( 'remove_coupon', rawurlencode( $coupon->get_code() ), Constants::is_defined( 'WOOCOMMERCE_CHECKOUT' ) ? wc_get_checkout_url() : wc_get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->get_code() ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
328
329 echo wp_kses( apply_filters( 'woocommerce_cart_totals_coupon_html', $coupon_html, $coupon, $discount_amount_html ), array_replace_recursive( wp_kses_allowed_html( 'post' ), array( 'a' => array( 'data-coupon' => true ) ) ) ); // phpcs:ignore PHPCompatibility.PHP.NewFunctions.array_replace_recursiveFound
330 }
331
332 /**
333 * Get order total html including inc tax if needed.
334 */
335 function wc_cart_totals_order_total_html() {
336 $value = '<strong>' . WC()->cart->get_total() . '</strong> ';
337
338 // If prices are tax inclusive, show taxes here.
339 if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
340 $tax_string_array = array();
341 $cart_tax_totals = WC()->cart->get_tax_totals();
342
343 if ( get_option( 'woocommerce_tax_total_display' ) === 'itemized' ) {
344 foreach ( $cart_tax_totals as $code => $tax ) {
345 $tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
346 }
347 } elseif ( ! empty( $cart_tax_totals ) ) {
348 $tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
349 }
350
351 if ( ! empty( $tax_string_array ) ) {
352 $taxable_address = WC()->customer->get_taxable_address();
353 if ( WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() ) {
354 $country = WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ];
355 /* translators: 1: tax amount 2: country name */
356 $tax_text = wp_kses_post( sprintf( __( '(includes %1$s estimated for %2$s)', 'woocommerce' ), implode( ', ', $tax_string_array ), $country ) );
357 } else {
358 /* translators: %s: tax amount */
359 $tax_text = wp_kses_post( sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) ) );
360 }
361
362 $value .= '<small class="includes_tax">' . $tax_text . '</small>';
363 }
364 }
365
366 echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
367 }
368
369 /**
370 * Get the fee value.
371 *
372 * @param object $fee Fee data.
373 */
374 function wc_cart_totals_fee_html( $fee ) {
375 $cart_totals_fee_html = WC()->cart->display_prices_including_tax() ? wc_price( $fee->total + $fee->tax ) : wc_price( $fee->total );
376
377 echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
378 }
379
380 /**
381 * Get a shipping methods full label including price.
382 *
383 * @param WC_Shipping_Rate $method Shipping method rate data.
384 * @return string
385 */
386 function wc_cart_totals_shipping_method_label( $method ) {
387 $label = $method->get_label();
388 $has_cost = 0 < $method->cost;
389 $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );
390
391 if ( $has_cost && ! $hide_cost ) {
392 if ( WC()->cart->display_prices_including_tax() ) {
393 $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
394 if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
395 $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
396 }
397 } else {
398 $label .= ': ' . wc_price( $method->cost );
399 if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
400 $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
401 }
402 }
403 }
404
405 return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
406 }
407
408 /**
409 * Round discount.
410 *
411 * @param double $value Amount to round.
412 * @param int $precision DP to round.
413 * @return float
414 */
415 function wc_cart_round_discount( $value, $precision ) {
416 return wc_round_discount( $value, $precision );
417 }
418
419 /**
420 * Gets chosen shipping method IDs from chosen_shipping_methods session, without instance IDs.
421 *
422 * @since 2.6.2
423 * @return string[]
424 */
425 function wc_get_chosen_shipping_method_ids() {
426 if ( ! is_callable( array( WC()->session, 'get' ) ) ) {
427 return array();
428 }
429
430 $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
431 $method_ids = array();
432
433 foreach ( $chosen_methods as $chosen_method ) {
434 if ( ! is_string( $chosen_method ) ) {
435 continue;
436 }
437 $chosen_method = explode( ':', $chosen_method );
438 $method_ids[] = current( $chosen_method );
439 }
440
441 return $method_ids;
442 }
443
444 /**
445 * Get chosen method for package from session.
446 *
447 * @since 3.2.0
448 * @param int $key Key of package.
449 * @param array $package Package data array.
450 * @return string|bool Either the chosen method ID or false if nothing is chosen yet.
451 */
452 function wc_get_chosen_shipping_method_for_package( $key, $package ) {
453 if ( ! is_callable( array( WC()->session, 'get' ) ) ) {
454 return false;
455 }
456
457 $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
458 $chosen_method = isset( $chosen_methods[ $key ] ) ? $chosen_methods[ $key ] : false;
459 $changed = wc_shipping_methods_have_changed( $key, $package );
460
461 // This is deprecated but here for BW compat. Remove in 4.0.0.
462 $method_counts = WC()->session->get( 'shipping_method_counts' );
463
464 if ( ! empty( $method_counts[ $key ] ) ) {
465 $method_count = absint( $method_counts[ $key ] );
466 } else {
467 $method_count = 0;
468 }
469
470 if ( ! isset( $package['rates'] ) || ! is_array( $package['rates'] ) ) {
471 $package['rates'] = array();
472 }
473
474 // If not set, not available, or available methods have changed, set to the DEFAULT option.
475 if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) || count( $package['rates'] ) !== $method_count ) {
476 $chosen_method = wc_get_default_shipping_method_for_package( $key, $package, $chosen_method );
477 $chosen_methods[ $key ] = $chosen_method;
478 $method_counts[ $key ] = count( $package['rates'] );
479
480 WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
481 WC()->session->set( 'shipping_method_counts', $method_counts );
482
483 /**
484 * Fires when a shipping method is chosen.
485 *
486 * @since 3.2.0
487 * @param string $chosen_method Chosen shipping method. e.g. flat_rate:1.
488 */
489 do_action( 'woocommerce_shipping_method_chosen', $chosen_method );
490 }
491 return $chosen_method;
492 }
493
494 /**
495 * Choose the default method for a package.
496 *
497 * @since 3.2.0
498 * @param int $key Key of package.
499 * @param array $package Package data array.
500 * @param string $chosen_method Chosen shipping method. e.g. flat_rate:1.
501 * @return string
502 */
503 function wc_get_default_shipping_method_for_package( $key, $package, $chosen_method ) {
504 $rate_keys = array_keys( $package['rates'] );
505 $local_pickup_method_ids = LocalPickupUtils::get_local_pickup_method_ids();
506
507 if ( 'shortcode' === WC()->cart->cart_context ) {
508 $default = current( $rate_keys );
509 } else {
510 // No default means that when you enter block checkout, shipping is chosen rather than pickup. We should only do this if there are shipping methods available other than local pickup.
511 $default = CartCheckoutUtils::shipping_methods_exist() ? '' : current( $rate_keys );
512
513 // Default to the first method in the package that isn't a local pickup method.
514 foreach ( $rate_keys as $rate_key ) {
515 $rate_method_id = current( explode( ':', $rate_key ) );
516 if ( ! in_array( $rate_method_id, $local_pickup_method_ids, true ) ) {
517 $default = $rate_key;
518 break;
519 }
520 }
521
522 // When shipping costs are hidden until an address is entered, don't auto-select pickup as the default.
523 // Without this, pickup gets silently selected because it's the only remaining rate after filtering.
524 if (
525 '' === $default
526 && 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' )
527 && WC()->customer instanceof \WC_Customer
528 && ! WC()->customer->has_full_shipping_address()
529 ) {
530 /**
531 * Filters the default shipping method for a package.
532 *
533 * @since 3.2.0
534 * @param string $default Default shipping method.
535 * @param array $rates Shipping rates.
536 * @param string $chosen_method Chosen method id.
537 */
538 return (string) apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method );
539 }
540 }
541
542 /**
543 * If the customer has selected local pickup, keep it selected if it's still in the package. We don't want to auto
544 * toggle between shipping and pickup even if available shipping methods are changed.
545 *
546 * This is important for block-based checkout where there is an explicit toggle between shipping and pickup.
547 */
548 $chosen_method_id = current( explode( ':', $chosen_method ) );
549 $chosen_method_exists = in_array( $chosen_method, $rate_keys, true );
550 $is_local_pickup_chosen = in_array( $chosen_method_id, $local_pickup_method_ids, true );
551
552 // Default to local pickup if its chosen already.
553 if ( $chosen_method_exists && $is_local_pickup_chosen ) {
554 $default = $chosen_method;
555
556 } else {
557 // Check coupons to see if free shipping is available. If it is, we'll use that method as the default.
558 $coupons = WC()->cart->get_coupons();
559 foreach ( $coupons as $coupon ) {
560 if ( $coupon->get_free_shipping() ) {
561 foreach ( $rate_keys as $rate_key ) {
562 if ( 0 === stripos( $rate_key, 'free_shipping' ) ) {
563 $default = $rate_key;
564 break;
565 }
566 }
567 break;
568 }
569 }
570 }
571
572 /**
573 * Filters the default shipping method for a package.
574 *
575 * @since 3.2.0
576 * @param string $default Default shipping method.
577 * @param array $rates Shipping rates.
578 * @param string $chosen_method Chosen method id.
579 */
580 return (string) apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method );
581 }
582
583 /**
584 * See if the methods have changed since the last request.
585 *
586 * @since 3.2.0
587 * @param int $key Key of package.
588 * @param array $package Package data array.
589 * @return bool
590 */
591 function wc_shipping_methods_have_changed( $key, $package ) {
592 if ( ! is_callable( array( WC()->session, 'get' ) ) ) {
593 return false;
594 }
595
596 // Lookup previous methods from session.
597 $previous_shipping_methods = WC()->session->get( 'previous_shipping_methods' );
598 // Get new and old rates.
599 $new_rates = array_keys( $package['rates'] );
600 $prev_rates = isset( $previous_shipping_methods[ $key ] ) ? $previous_shipping_methods[ $key ] : false;
601 // Update session.
602 $previous_shipping_methods[ $key ] = $new_rates;
603 WC()->session->set( 'previous_shipping_methods', $previous_shipping_methods );
604 return $new_rates !== $prev_rates;
605 }
606
607 /**
608 * Gets a hash of important product data that when changed should cause cart items to be invalidated.
609 *
610 * The woocommerce_cart_item_data_to_validate filter can be used to add custom properties.
611 *
612 * @param WC_Product $product Product object.
613 * @return string
614 */
615 function wc_get_cart_item_data_hash( $product ) {
616 return md5(
617 wp_json_encode(
618 apply_filters(
619 'woocommerce_cart_item_data_to_validate',
620 array(
621 'type' => $product->get_type(),
622 'attributes' => ProductType::VARIATION === $product->get_type() ? $product->get_variation_attributes() : '',
623 ),
624 $product
625 )
626 )
627 );
628 }
629