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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 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 1 year ago admin 1 year ago blocks 1 year ago cli 1 year ago customizer 2 years ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 2 years ago libraries 1 year ago log-handlers 2 years ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year ago theme-support 2 years ago tracks 1 year ago traits 5 years ago walkers 5 years ago wccom-site 1 year ago widgets 1 year ago class-wc-ajax.php 2 years ago class-wc-auth.php 1 year ago class-wc-autoloader.php 1 year ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 year ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 1 year ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 years ago class-wc-cart-totals.php 2 years ago class-wc-cart.php 1 year ago class-wc-checkout.php 1 year ago class-wc-cli.php 1 year ago class-wc-comments.php 3 years ago class-wc-countries.php 2 years ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 2 years 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 3 years ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 year ago class-wc-emails.php 1 year ago class-wc-embed.php 5 years ago class-wc-form-handler.php 1 year ago class-wc-frontend-scripts.php 1 year ago class-wc-geo-ip.php 1 year ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 year ago class-wc-https.php 2 years ago class-wc-install.php 1 year ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 1 year ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 2 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 year ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 year ago class-wc-order-item-shipping.php 2 years ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 1 year ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 2 years ago class-wc-order.php 1 year ago class-wc-payment-gateways.php 2 years ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 year ago class-wc-post-types.php 1 year ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 2 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 year ago class-wc-product-variation.php 1 year ago class-wc-query.php 1 year ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 2 years ago class-wc-shipping-rate.php 3 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 2 years ago class-wc-structured-data.php 1 year ago class-wc-tax.php 2 years ago class-wc-template-loader.php 2 years ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 year ago class-woocommerce.php 1 year ago wc-account-functions.php 1 year ago wc-attribute-functions.php 1 year ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 year ago wc-conditional-functions.php 2 years ago wc-core-functions.php 1 year ago wc-coupon-functions.php 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 1 year ago wc-notice-functions.php 2 years ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years ago wc-page-functions.php 1 year ago wc-product-functions.php 1 year ago wc-rest-functions.php 1 year ago wc-stock-functions.php 1 year ago wc-template-functions.php 1 year ago wc-template-hooks.php 1 year ago wc-term-functions.php 3 years ago wc-update-functions.php 1 year ago wc-user-functions.php 1 year ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
wc-cart-functions.php
586 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\StoreApi\Utilities\LocalPickupUtils;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Prevent password protected products being added to the cart.
18 *
19 * @param bool $passed Validation.
20 * @param int $product_id Product ID.
21 * @return bool
22 */
23 function wc_protected_product_add_to_cart( $passed, $product_id ) {
24 if ( post_password_required( $product_id ) ) {
25 $passed = false;
26 wc_add_notice( __( 'This product is protected and cannot be purchased.', 'woocommerce' ), 'error' );
27 }
28 return $passed;
29 }
30 add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );
31
32 /**
33 * Clears the cart session when called.
34 */
35 function wc_empty_cart() {
36 if ( ! isset( WC()->cart ) || '' === WC()->cart ) {
37 WC()->cart = new WC_Cart();
38 }
39 WC()->cart->empty_cart( false );
40 }
41
42 /**
43 * Load the persistent cart.
44 *
45 * @param string $user_login User login.
46 * @param WP_User $user User data.
47 * @deprecated 2.3
48 */
49 function wc_load_persistent_cart( $user_login, $user ) {
50 if ( ! $user || ! apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
51 return;
52 }
53
54 $saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
55
56 if ( ! $saved_cart ) {
57 return;
58 }
59
60 $cart = WC()->session->cart;
61
62 if ( empty( $cart ) || ! is_array( $cart ) || 0 === count( $cart ) ) {
63 WC()->session->cart = $saved_cart['cart'];
64 }
65 }
66
67 /**
68 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
69 *
70 * Do not use for redirects, use {@see wp_get_referer()} instead.
71 *
72 * @since 2.6.1
73 * @return string|false Referer URL on success, false on failure.
74 */
75 function wc_get_raw_referer() {
76 if ( function_exists( 'wp_get_raw_referer' ) ) {
77 return wp_get_raw_referer();
78 }
79
80 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { // WPCS: input var ok, CSRF ok.
81 return wp_unslash( $_REQUEST['_wp_http_referer'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
82 } elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { // WPCS: input var ok, CSRF ok.
83 return wp_unslash( $_SERVER['HTTP_REFERER'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
84 }
85
86 return false;
87 }
88
89 /**
90 * Add to cart messages.
91 *
92 * @param int|array $products Product ID list or single product ID.
93 * @param bool $show_qty Should quantities be shown? Added in 2.6.0.
94 * @param bool $return Return message rather than add it.
95 *
96 * @return mixed
97 */
98 function wc_add_to_cart_message( $products, $show_qty = false, $return = false ) {
99 $titles = array();
100 $count = 0;
101
102 if ( ! is_array( $products ) ) {
103 $products = array( $products => 1 );
104 $show_qty = false;
105 }
106
107 if ( ! $show_qty ) {
108 $products = array_fill_keys( array_keys( $products ), 1 );
109 }
110
111 $product_id = null;
112 foreach ( $products as $product_id => $qty ) {
113 /* translators: %s: product name */
114 $titles[] = apply_filters( 'woocommerce_add_to_cart_qty_html', ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ), $product_id ) . apply_filters( 'woocommerce_add_to_cart_item_name_in_quotes', sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) ), $product_id );
115 $count += $qty;
116 }
117
118 $titles = array_filter( $titles );
119 /* translators: %s: product name */
120 $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 ) );
121
122 // Output success messages.
123 $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
124 if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
125 $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' ) );
126 $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' ) );
127 } else {
128 $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' ) );
129 }
130
131 if ( has_filter( 'wc_add_to_cart_message' ) ) {
132 wc_deprecated_function( 'The wc_add_to_cart_message filter', '3.0', 'wc_add_to_cart_message_html' );
133 $message = apply_filters( 'wc_add_to_cart_message', $message, $product_id );
134 }
135
136 $message = apply_filters( 'wc_add_to_cart_message_html', $message, $products, $show_qty );
137
138 if ( $return ) {
139 return $message;
140 } else {
141 wc_add_notice( $message, apply_filters( 'woocommerce_add_to_cart_notice_type', 'success' ) );
142 }
143 }
144
145 /**
146 * Comma separate a list of item names, and replace final comma with 'and'.
147 *
148 * @param array $items Cart items.
149 * @return string
150 */
151 function wc_format_list_of_items( $items ) {
152 $item_string = '';
153
154 foreach ( $items as $key => $item ) {
155 $item_string .= $item;
156
157 if ( count( $items ) === $key + 2 ) {
158 $item_string .= ' ' . __( 'and', 'woocommerce' ) . ' ';
159 } elseif ( count( $items ) !== $key + 1 ) {
160 $item_string .= ', ';
161 }
162 }
163
164 return $item_string;
165 }
166
167 /**
168 * Clear cart after payment.
169 */
170 function wc_clear_cart_after_payment() {
171 global $wp;
172
173 $should_clear_cart_after_payment = false;
174 $after_payment = false;
175
176 // If the order has been received, clear the cart.
177 if ( ! empty( $wp->query_vars['order-received'] ) ) {
178
179 $order_id = absint( $wp->query_vars['order-received'] );
180 $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok.
181
182 if ( $order_id > 0 ) {
183 $order = wc_get_order( $order_id );
184
185 if ( $order instanceof WC_Order && hash_equals( $order->get_order_key(), $order_key ) ) {
186 $should_clear_cart_after_payment = true;
187 $after_payment = true;
188 }
189 }
190 }
191
192 // If the order is awaiting payment, and we haven't already decided to clear the cart, check the order status.
193 if ( is_object( WC()->session ) && WC()->session->order_awaiting_payment > 0 && ! $should_clear_cart_after_payment ) {
194 $order = wc_get_order( WC()->session->order_awaiting_payment );
195
196 if ( $order instanceof WC_Order && $order->get_id() > 0 ) {
197 // If the order status is neither pending, failed, nor cancelled, the order must have gone through.
198 $should_clear_cart_after_payment = ! $order->has_status( array( 'failed', 'pending', 'cancelled' ) );
199 $after_payment = true;
200 }
201 }
202
203 // If it doesn't look like a payment happened, bail early.
204 if ( ! $after_payment ) {
205 return;
206 }
207
208 /**
209 * Determine whether the cart should be cleared after payment.
210 *
211 * @since 9.3.0
212 * @param bool $should_clear_cart_after_payment Whether the cart should be cleared after payment.
213 */
214 $should_clear_cart_after_payment = apply_filters( 'woocommerce_should_clear_cart_after_payment', $should_clear_cart_after_payment );
215
216 if ( $should_clear_cart_after_payment ) {
217 WC()->cart->empty_cart();
218 }
219 }
220 add_action( 'template_redirect', 'wc_clear_cart_after_payment', 20 );
221
222 /**
223 * Get the subtotal.
224 */
225 function wc_cart_totals_subtotal_html() {
226 echo WC()->cart->get_cart_subtotal(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
227 }
228
229 /**
230 * Get shipping methods.
231 */
232 function wc_cart_totals_shipping_html() {
233 $packages = WC()->shipping()->get_packages();
234 $first = true;
235
236 foreach ( $packages as $i => $package ) {
237 $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
238 $product_names = array();
239
240 if ( count( $packages ) > 1 ) {
241 foreach ( $package['contents'] as $item_id => $values ) {
242 $product_names[ $item_id ] = $values['data']->get_name() . ' &times;' . $values['quantity'];
243 }
244 $product_names = apply_filters( 'woocommerce_shipping_package_details_array', $product_names, $package );
245 }
246
247 wc_get_template(
248 'cart/cart-shipping.php',
249 array(
250 'package' => $package,
251 'available_methods' => $package['rates'],
252 'show_package_details' => count( $packages ) > 1,
253 'show_shipping_calculator' => is_cart() && apply_filters( 'woocommerce_shipping_show_shipping_calculator', $first, $i, $package ),
254 'package_details' => implode( ', ', $product_names ),
255 /* translators: %d: shipping package number */
256 'package_name' => apply_filters( 'woocommerce_shipping_package_name', ( ( $i + 1 ) > 1 ) ? sprintf( _x( 'Shipping %d', 'shipping packages', 'woocommerce' ), ( $i + 1 ) ) : _x( 'Shipping', 'shipping packages', 'woocommerce' ), $i, $package ),
257 'index' => $i,
258 'chosen_method' => $chosen_method,
259 'formatted_destination' => WC()->countries->get_formatted_address( $package['destination'], ', ' ),
260 'has_calculated_shipping' => WC()->customer->has_calculated_shipping(),
261 )
262 );
263
264 $first = false;
265 }
266 }
267
268 /**
269 * Get taxes total.
270 */
271 function wc_cart_totals_taxes_total_html() {
272 echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
273 }
274
275 /**
276 * Get a coupon label.
277 *
278 * @param string|WC_Coupon $coupon Coupon data or code.
279 * @param bool $echo Echo or return.
280 *
281 * @return string
282 */
283 function wc_cart_totals_coupon_label( $coupon, $echo = true ) {
284 if ( is_string( $coupon ) ) {
285 $coupon = new WC_Coupon( $coupon );
286 }
287
288 /* translators: %s: coupon code */
289 $label = apply_filters( 'woocommerce_cart_totals_coupon_label', sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ), $coupon->get_code() ), $coupon );
290
291 if ( $echo ) {
292 echo $label; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
293 } else {
294 return $label;
295 }
296 }
297
298 /**
299 * Get coupon display HTML.
300 *
301 * @param string|WC_Coupon $coupon Coupon data or code.
302 */
303 function wc_cart_totals_coupon_html( $coupon ) {
304 if ( is_string( $coupon ) ) {
305 $coupon = new WC_Coupon( $coupon );
306 }
307
308 $amount = WC()->cart->get_coupon_discount_amount( $coupon->get_code(), WC()->cart->display_cart_ex_tax );
309 $discount_amount_html = '-' . wc_price( $amount );
310
311 if ( $coupon->get_free_shipping() && empty( $amount ) ) {
312 $discount_amount_html = __( 'Free shipping coupon', 'woocommerce' );
313 }
314
315 $discount_amount_html = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_amount_html, $coupon );
316 $coupon_html = $discount_amount_html . ' <a 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>';
317
318 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
319 }
320
321 /**
322 * Get order total html including inc tax if needed.
323 */
324 function wc_cart_totals_order_total_html() {
325 $value = '<strong>' . WC()->cart->get_total() . '</strong> ';
326
327 // If prices are tax inclusive, show taxes here.
328 if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
329 $tax_string_array = array();
330 $cart_tax_totals = WC()->cart->get_tax_totals();
331
332 if ( get_option( 'woocommerce_tax_total_display' ) === 'itemized' ) {
333 foreach ( $cart_tax_totals as $code => $tax ) {
334 $tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
335 }
336 } elseif ( ! empty( $cart_tax_totals ) ) {
337 $tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
338 }
339
340 if ( ! empty( $tax_string_array ) ) {
341 $taxable_address = WC()->customer->get_taxable_address();
342 if ( WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() ) {
343 $country = WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ];
344 /* translators: 1: tax amount 2: country name */
345 $tax_text = wp_kses_post( sprintf( __( '(includes %1$s estimated for %2$s)', 'woocommerce' ), implode( ', ', $tax_string_array ), $country ) );
346 } else {
347 /* translators: %s: tax amount */
348 $tax_text = wp_kses_post( sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) ) );
349 }
350
351 $value .= '<small class="includes_tax">' . $tax_text . '</small>';
352 }
353 }
354
355 echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
356 }
357
358 /**
359 * Get the fee value.
360 *
361 * @param object $fee Fee data.
362 */
363 function wc_cart_totals_fee_html( $fee ) {
364 $cart_totals_fee_html = WC()->cart->display_prices_including_tax() ? wc_price( $fee->total + $fee->tax ) : wc_price( $fee->total );
365
366 echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
367 }
368
369 /**
370 * Get a shipping methods full label including price.
371 *
372 * @param WC_Shipping_Rate $method Shipping method rate data.
373 * @return string
374 */
375 function wc_cart_totals_shipping_method_label( $method ) {
376 $label = $method->get_label();
377 $has_cost = 0 < $method->cost;
378 $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );
379
380 if ( $has_cost && ! $hide_cost ) {
381 if ( WC()->cart->display_prices_including_tax() ) {
382 $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
383 if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
384 $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
385 }
386 } else {
387 $label .= ': ' . wc_price( $method->cost );
388 if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
389 $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
390 }
391 }
392 }
393
394 return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
395 }
396
397 /**
398 * Round discount.
399 *
400 * @param double $value Amount to round.
401 * @param int $precision DP to round.
402 * @return float
403 */
404 function wc_cart_round_discount( $value, $precision ) {
405 return wc_round_discount( $value, $precision );
406 }
407
408 /**
409 * Gets chosen shipping method IDs from chosen_shipping_methods session, without instance IDs.
410 *
411 * @since 2.6.2
412 * @return string[]
413 */
414 function wc_get_chosen_shipping_method_ids() {
415 if ( ! is_callable( array( WC()->session, 'get' ) ) ) {
416 return array();
417 }
418
419 $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
420 $method_ids = array();
421
422 foreach ( $chosen_methods as $chosen_method ) {
423 if ( ! is_string( $chosen_method ) ) {
424 continue;
425 }
426 $chosen_method = explode( ':', $chosen_method );
427 $method_ids[] = current( $chosen_method );
428 }
429
430 return $method_ids;
431 }
432
433 /**
434 * Get chosen method for package from session.
435 *
436 * @since 3.2.0
437 * @param int $key Key of package.
438 * @param array $package Package data array.
439 * @return string|bool Either the chosen method ID or false if nothing is chosen yet.
440 */
441 function wc_get_chosen_shipping_method_for_package( $key, $package ) {
442 if ( ! is_callable( array( WC()->session, 'get' ) ) ) {
443 return false;
444 }
445
446 $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
447 $chosen_method = isset( $chosen_methods[ $key ] ) ? $chosen_methods[ $key ] : false;
448 $changed = wc_shipping_methods_have_changed( $key, $package );
449
450 // This is deprecated but here for BW compat. Remove in 4.0.0.
451 $method_counts = WC()->session->get( 'shipping_method_counts' );
452
453 if ( ! empty( $method_counts[ $key ] ) ) {
454 $method_count = absint( $method_counts[ $key ] );
455 } else {
456 $method_count = 0;
457 }
458
459 if ( ! isset( $package['rates'] ) || ! is_array( $package['rates'] ) ) {
460 $package['rates'] = array();
461 }
462
463 // If not set, not available, or available methods have changed, set to the DEFAULT option.
464 if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) || count( $package['rates'] ) !== $method_count ) {
465 $chosen_method = wc_get_default_shipping_method_for_package( $key, $package, $chosen_method );
466 $chosen_methods[ $key ] = $chosen_method;
467 $method_counts[ $key ] = count( $package['rates'] );
468
469 WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
470 WC()->session->set( 'shipping_method_counts', $method_counts );
471
472 /**
473 * Fires when a shipping method is chosen.
474 *
475 * @since 3.2.0
476 * @param string $chosen_method Chosen shipping method. e.g. flat_rate:1.
477 */
478 do_action( 'woocommerce_shipping_method_chosen', $chosen_method );
479 }
480 return $chosen_method;
481 }
482
483 /**
484 * Choose the default method for a package.
485 *
486 * @since 3.2.0
487 * @param int $key Key of package.
488 * @param array $package Package data array.
489 * @param string $chosen_method Chosen shipping method. e.g. flat_rate:1.
490 * @return string
491 */
492 function wc_get_default_shipping_method_for_package( $key, $package, $chosen_method ) {
493 $chosen_method_id = current( explode( ':', $chosen_method ) );
494 $rate_keys = array_keys( $package['rates'] );
495 $chosen_method_exists = in_array( $chosen_method, $rate_keys, true );
496
497 /**
498 * If the customer has selected local pickup, keep it selected if it's still in the package. We don't want to auto
499 * toggle between shipping and pickup even if available shipping methods are changed.
500 *
501 * This is important for block-based checkout where there is an explicit toggle between shipping and pickup.
502 */
503 $local_pickup_method_ids = LocalPickupUtils::get_local_pickup_method_ids();
504 $is_local_pickup_chosen = in_array( $chosen_method_id, $local_pickup_method_ids, true );
505
506 // Default to the first method in the package. This can be sorted in the backend by the merchant.
507 $default = current( $rate_keys );
508
509 // Default to local pickup if its chosen already.
510 if ( $chosen_method_exists && $is_local_pickup_chosen ) {
511 $default = $chosen_method;
512
513 } else {
514 // Check coupons to see if free shipping is available. If it is, we'll use that method as the default.
515 $coupons = WC()->cart->get_coupons();
516 foreach ( $coupons as $coupon ) {
517 if ( $coupon->get_free_shipping() ) {
518 foreach ( $rate_keys as $rate_key ) {
519 if ( 0 === stripos( $rate_key, 'free_shipping' ) ) {
520 $default = $rate_key;
521 break;
522 }
523 }
524 break;
525 }
526 }
527 }
528
529 /**
530 * Filters the default shipping method for a package.
531 *
532 * @since 3.2.0
533 * @param string $default Default shipping method.
534 * @param array $rates Shipping rates.
535 * @param string $chosen_method Chosen method id.
536 */
537 return (string) apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method );
538 }
539
540 /**
541 * See if the methods have changed since the last request.
542 *
543 * @since 3.2.0
544 * @param int $key Key of package.
545 * @param array $package Package data array.
546 * @return bool
547 */
548 function wc_shipping_methods_have_changed( $key, $package ) {
549 if ( ! is_callable( array( WC()->session, 'get' ) ) ) {
550 return false;
551 }
552
553 // Lookup previous methods from session.
554 $previous_shipping_methods = WC()->session->get( 'previous_shipping_methods' );
555 // Get new and old rates.
556 $new_rates = array_keys( $package['rates'] );
557 $prev_rates = isset( $previous_shipping_methods[ $key ] ) ? $previous_shipping_methods[ $key ] : false;
558 // Update session.
559 $previous_shipping_methods[ $key ] = $new_rates;
560 WC()->session->set( 'previous_shipping_methods', $previous_shipping_methods );
561 return $new_rates !== $prev_rates;
562 }
563
564 /**
565 * Gets a hash of important product data that when changed should cause cart items to be invalidated.
566 *
567 * The woocommerce_cart_item_data_to_validate filter can be used to add custom properties.
568 *
569 * @param WC_Product $product Product object.
570 * @return string
571 */
572 function wc_get_cart_item_data_hash( $product ) {
573 return md5(
574 wp_json_encode(
575 apply_filters(
576 'woocommerce_cart_item_data_to_validate',
577 array(
578 'type' => $product->get_type(),
579 'attributes' => 'variation' === $product->get_type() ? $product->get_variation_attributes() : '',
580 ),
581 $product
582 )
583 )
584 );
585 }
586