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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 5 years ago admin 5 years ago cli 5 years ago customizer 5 years ago data-stores 4 years ago emails 5 years ago export 5 years ago gateways 5 years ago import 5 years ago integrations 5 years ago interfaces 5 years ago legacy 5 years ago libraries 5 years ago log-handlers 5 years ago payment-tokens 5 years ago queue 5 years ago shipping 5 years ago shortcodes 5 years ago theme-support 5 years ago tracks 5 years ago traits 5 years ago walkers 5 years ago wccom-site 5 years ago widgets 5 years ago class-wc-ajax.php 5 years ago class-wc-api.php 5 years ago class-wc-auth.php 5 years ago class-wc-autoloader.php 5 years ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 5 years ago class-wc-cart-fees.php 5 years ago class-wc-cart-session.php 5 years ago class-wc-cart-totals.php 5 years ago class-wc-cart.php 5 years ago class-wc-checkout.php 5 years ago class-wc-cli.php 5 years ago class-wc-comments.php 5 years ago class-wc-countries.php 5 years ago class-wc-coupon.php 5 years ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 5 years ago class-wc-customer.php 5 years ago class-wc-data-exception.php 5 years ago class-wc-data-store.php 5 years ago class-wc-datetime.php 5 years ago class-wc-deprecated-action-hooks.php 5 years ago class-wc-deprecated-filter-hooks.php 5 years ago class-wc-discounts.php 5 years ago class-wc-download-handler.php 5 years ago class-wc-emails.php 5 years ago class-wc-embed.php 5 years ago class-wc-form-handler.php 5 years ago class-wc-frontend-scripts.php 5 years ago class-wc-geo-ip.php 5 years ago class-wc-geolite-integration.php 5 years ago class-wc-geolocation.php 5 years ago class-wc-https.php 5 years ago class-wc-install.php 4 years ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 5 years ago class-wc-logger.php 5 years ago class-wc-meta-data.php 5 years ago class-wc-order-factory.php 5 years ago class-wc-order-item-coupon.php 5 years ago class-wc-order-item-fee.php 5 years ago class-wc-order-item-meta.php 5 years ago class-wc-order-item-product.php 5 years ago class-wc-order-item-shipping.php 5 years ago class-wc-order-item-tax.php 5 years ago class-wc-order-item.php 5 years ago class-wc-order-query.php 5 years ago class-wc-order-refund.php 5 years ago class-wc-order.php 5 years ago class-wc-payment-gateways.php 5 years ago class-wc-payment-tokens.php 5 years ago class-wc-post-data.php 5 years ago class-wc-post-types.php 5 years ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 5 years ago class-wc-privacy-exporters.php 5 years ago class-wc-privacy.php 5 years ago class-wc-product-attribute.php 5 years ago class-wc-product-download.php 5 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 5 years ago class-wc-product-grouped.php 5 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 5 years ago class-wc-product-variable.php 5 years ago class-wc-product-variation.php 5 years ago class-wc-query.php 5 years ago class-wc-rate-limiter.php 5 years ago class-wc-regenerate-images-request.php 5 years ago class-wc-regenerate-images.php 5 years ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 5 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 5 years ago class-wc-shipping-rate.php 5 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 5 years ago class-wc-shortcodes.php 5 years ago class-wc-structured-data.php 5 years ago class-wc-tax.php 5 years ago class-wc-template-loader.php 5 years ago class-wc-tracker.php 5 years ago class-wc-validation.php 5 years ago class-wc-webhook.php 5 years ago class-woocommerce.php 4 years ago wc-account-functions.php 5 years ago wc-attribute-functions.php 5 years ago wc-cart-functions.php 5 years ago wc-conditional-functions.php 5 years ago wc-core-functions.php 5 years ago wc-coupon-functions.php 5 years ago wc-deprecated-functions.php 5 years ago wc-formatting-functions.php 5 years ago wc-notice-functions.php 5 years ago wc-order-functions.php 5 years ago wc-order-item-functions.php 5 years ago wc-page-functions.php 5 years ago wc-product-functions.php 5 years ago wc-rest-functions.php 5 years ago wc-stock-functions.php 5 years ago wc-template-functions.php 5 years ago wc-template-hooks.php 5 years ago wc-term-functions.php 5 years ago wc-update-functions.php 4 years ago wc-user-functions.php 5 years ago wc-webhook-functions.php 5 years ago wc-widget-functions.php 5 years ago
wc-cart-functions.php
505 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
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Prevent password protected products being added to the cart.
17 *
18 * @param bool $passed Validation.
19 * @param int $product_id Product ID.
20 * @return bool
21 */
22 function wc_protected_product_add_to_cart( $passed, $product_id ) {
23 if ( post_password_required( $product_id ) ) {
24 $passed = false;
25 wc_add_notice( __( 'This product is protected and cannot be purchased.', 'woocommerce' ), 'error' );
26 }
27 return $passed;
28 }
29 add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );
30
31 /**
32 * Clears the cart session when called.
33 */
34 function wc_empty_cart() {
35 if ( ! isset( WC()->cart ) || '' === WC()->cart ) {
36 WC()->cart = new WC_Cart();
37 }
38 WC()->cart->empty_cart( false );
39 }
40
41 /**
42 * Load the persistent cart.
43 *
44 * @param string $user_login User login.
45 * @param WP_User $user User data.
46 * @deprecated 2.3
47 */
48 function wc_load_persistent_cart( $user_login, $user ) {
49 if ( ! $user || ! apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
50 return;
51 }
52
53 $saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
54
55 if ( ! $saved_cart ) {
56 return;
57 }
58
59 $cart = WC()->session->cart;
60
61 if ( empty( $cart ) || ! is_array( $cart ) || 0 === count( $cart ) ) {
62 WC()->session->cart = $saved_cart['cart'];
63 }
64 }
65
66 /**
67 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
68 *
69 * Do not use for redirects, use {@see wp_get_referer()} instead.
70 *
71 * @since 2.6.1
72 * @return string|false Referer URL on success, false on failure.
73 */
74 function wc_get_raw_referer() {
75 if ( function_exists( 'wp_get_raw_referer' ) ) {
76 return wp_get_raw_referer();
77 }
78
79 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { // WPCS: input var ok, CSRF ok.
80 return wp_unslash( $_REQUEST['_wp_http_referer'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
81 } elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { // WPCS: input var ok, CSRF ok.
82 return wp_unslash( $_SERVER['HTTP_REFERER'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
83 }
84
85 return false;
86 }
87
88 /**
89 * Add to cart messages.
90 *
91 * @param int|array $products Product ID list or single product ID.
92 * @param bool $show_qty Should qty's be shown? Added in 2.6.0.
93 * @param bool $return Return message rather than add it.
94 *
95 * @return mixed
96 */
97 function wc_add_to_cart_message( $products, $show_qty = false, $return = false ) {
98 $titles = array();
99 $count = 0;
100
101 if ( ! is_array( $products ) ) {
102 $products = array( $products => 1 );
103 $show_qty = false;
104 }
105
106 if ( ! $show_qty ) {
107 $products = array_fill_keys( array_keys( $products ), 1 );
108 }
109
110 foreach ( $products as $product_id => $qty ) {
111 /* translators: %s: product name */
112 $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 );
113 $count += $qty;
114 }
115
116 $titles = array_filter( $titles );
117 /* translators: %s: product name */
118 $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 ) );
119
120 // Output success messages.
121 if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
122 $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' ) );
123 $message = sprintf( '<a href="%s" tabindex="1" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
124 } else {
125 $message = sprintf( '<a href="%s" tabindex="1" class="button wc-forward">%s</a> %s', esc_url( wc_get_cart_url() ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
126 }
127
128 if ( has_filter( 'wc_add_to_cart_message' ) ) {
129 wc_deprecated_function( 'The wc_add_to_cart_message filter', '3.0', 'wc_add_to_cart_message_html' );
130 $message = apply_filters( 'wc_add_to_cart_message', $message, $product_id );
131 }
132
133 $message = apply_filters( 'wc_add_to_cart_message_html', $message, $products, $show_qty );
134
135 if ( $return ) {
136 return $message;
137 } else {
138 wc_add_notice( $message, apply_filters( 'woocommerce_add_to_cart_notice_type', 'success' ) );
139 }
140 }
141
142 /**
143 * Comma separate a list of item names, and replace final comma with 'and'.
144 *
145 * @param array $items Cart items.
146 * @return string
147 */
148 function wc_format_list_of_items( $items ) {
149 $item_string = '';
150
151 foreach ( $items as $key => $item ) {
152 $item_string .= $item;
153
154 if ( count( $items ) === $key + 2 ) {
155 $item_string .= ' ' . __( 'and', 'woocommerce' ) . ' ';
156 } elseif ( count( $items ) !== $key + 1 ) {
157 $item_string .= ', ';
158 }
159 }
160
161 return $item_string;
162 }
163
164 /**
165 * Clear cart after payment.
166 */
167 function wc_clear_cart_after_payment() {
168 global $wp;
169
170 if ( ! empty( $wp->query_vars['order-received'] ) ) {
171
172 $order_id = absint( $wp->query_vars['order-received'] );
173 $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok.
174
175 if ( $order_id > 0 ) {
176 $order = wc_get_order( $order_id );
177
178 if ( $order && hash_equals( $order->get_order_key(), $order_key ) ) {
179 WC()->cart->empty_cart();
180 }
181 }
182 }
183
184 if ( WC()->session->order_awaiting_payment > 0 ) {
185 $order = wc_get_order( WC()->session->order_awaiting_payment );
186
187 if ( $order && $order->get_id() > 0 ) {
188 // If the order has not failed, or is not pending, the order must have gone through.
189 if ( ! $order->has_status( array( 'failed', 'pending', 'cancelled' ) ) ) {
190 WC()->cart->empty_cart();
191 }
192 }
193 }
194 }
195 add_action( 'get_header', 'wc_clear_cart_after_payment' );
196
197 /**
198 * Get the subtotal.
199 */
200 function wc_cart_totals_subtotal_html() {
201 echo WC()->cart->get_cart_subtotal(); // WPCS: XSS ok.
202 }
203
204 /**
205 * Get shipping methods.
206 */
207 function wc_cart_totals_shipping_html() {
208 $packages = WC()->shipping()->get_packages();
209 $first = true;
210
211 foreach ( $packages as $i => $package ) {
212 $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
213 $product_names = array();
214
215 if ( count( $packages ) > 1 ) {
216 foreach ( $package['contents'] as $item_id => $values ) {
217 $product_names[ $item_id ] = $values['data']->get_name() . ' &times;' . $values['quantity'];
218 }
219 $product_names = apply_filters( 'woocommerce_shipping_package_details_array', $product_names, $package );
220 }
221
222 wc_get_template(
223 'cart/cart-shipping.php',
224 array(
225 'package' => $package,
226 'available_methods' => $package['rates'],
227 'show_package_details' => count( $packages ) > 1,
228 'show_shipping_calculator' => is_cart() && apply_filters( 'woocommerce_shipping_show_shipping_calculator', $first, $i, $package ),
229 'package_details' => implode( ', ', $product_names ),
230 /* translators: %d: shipping package number */
231 '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 ),
232 'index' => $i,
233 'chosen_method' => $chosen_method,
234 'formatted_destination' => WC()->countries->get_formatted_address( $package['destination'], ', ' ),
235 'has_calculated_shipping' => WC()->customer->has_calculated_shipping(),
236 )
237 );
238
239 $first = false;
240 }
241 }
242
243 /**
244 * Get taxes total.
245 */
246 function wc_cart_totals_taxes_total_html() {
247 echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) ); // WPCS: XSS ok.
248 }
249
250 /**
251 * Get a coupon label.
252 *
253 * @param string|WC_Coupon $coupon Coupon data or code.
254 * @param bool $echo Echo or return.
255 *
256 * @return string
257 */
258 function wc_cart_totals_coupon_label( $coupon, $echo = true ) {
259 if ( is_string( $coupon ) ) {
260 $coupon = new WC_Coupon( $coupon );
261 }
262
263 /* translators: %s: coupon code */
264 $label = apply_filters( 'woocommerce_cart_totals_coupon_label', sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ), $coupon->get_code() ), $coupon );
265
266 if ( $echo ) {
267 echo $label; // WPCS: XSS ok.
268 } else {
269 return $label;
270 }
271 }
272
273 /**
274 * Get coupon display HTML.
275 *
276 * @param string|WC_Coupon $coupon Coupon data or code.
277 */
278 function wc_cart_totals_coupon_html( $coupon ) {
279 if ( is_string( $coupon ) ) {
280 $coupon = new WC_Coupon( $coupon );
281 }
282
283 $discount_amount_html = '';
284
285 $amount = WC()->cart->get_coupon_discount_amount( $coupon->get_code(), WC()->cart->display_cart_ex_tax );
286 $discount_amount_html = '-' . wc_price( $amount );
287
288 if ( $coupon->get_free_shipping() && empty( $amount ) ) {
289 $discount_amount_html = __( 'Free shipping coupon', 'woocommerce' );
290 }
291
292 $discount_amount_html = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_amount_html, $coupon );
293 $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>';
294
295 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
296 }
297
298 /**
299 * Get order total html including inc tax if needed.
300 */
301 function wc_cart_totals_order_total_html() {
302 $value = '<strong>' . WC()->cart->get_total() . '</strong> ';
303
304 // If prices are tax inclusive, show taxes here.
305 if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
306 $tax_string_array = array();
307 $cart_tax_totals = WC()->cart->get_tax_totals();
308
309 if ( get_option( 'woocommerce_tax_total_display' ) === 'itemized' ) {
310 foreach ( $cart_tax_totals as $code => $tax ) {
311 $tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
312 }
313 } elseif ( ! empty( $cart_tax_totals ) ) {
314 $tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
315 }
316
317 if ( ! empty( $tax_string_array ) ) {
318 $taxable_address = WC()->customer->get_taxable_address();
319 /* translators: %s: country name */
320 $estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() ? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] ) : '';
321 $value .= '<small class="includes_tax">('
322 /* translators: includes tax information */
323 . esc_html__( 'includes', 'woocommerce' )
324 . ' '
325 . wp_kses_post( implode( ', ', $tax_string_array ) )
326 . esc_html( $estimated_text )
327 . ')</small>';
328 }
329 }
330
331 echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value ); // WPCS: XSS ok.
332 }
333
334 /**
335 * Get the fee value.
336 *
337 * @param object $fee Fee data.
338 */
339 function wc_cart_totals_fee_html( $fee ) {
340 $cart_totals_fee_html = WC()->cart->display_prices_including_tax() ? wc_price( $fee->total + $fee->tax ) : wc_price( $fee->total );
341
342 echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee ); // WPCS: XSS ok.
343 }
344
345 /**
346 * Get a shipping methods full label including price.
347 *
348 * @param WC_Shipping_Rate $method Shipping method rate data.
349 * @return string
350 */
351 function wc_cart_totals_shipping_method_label( $method ) {
352 $label = $method->get_label();
353 $has_cost = 0 < $method->cost;
354 $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );
355
356 if ( $has_cost && ! $hide_cost ) {
357 if ( WC()->cart->display_prices_including_tax() ) {
358 $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
359 if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
360 $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
361 }
362 } else {
363 $label .= ': ' . wc_price( $method->cost );
364 if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
365 $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
366 }
367 }
368 }
369
370 return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
371 }
372
373 /**
374 * Round discount.
375 *
376 * @param double $value Amount to round.
377 * @param int $precision DP to round.
378 * @return float
379 */
380 function wc_cart_round_discount( $value, $precision ) {
381 return wc_round_discount( $value, $precision );
382 }
383
384 /**
385 * Gets chosen shipping method IDs from chosen_shipping_methods session, without instance IDs.
386 *
387 * @since 2.6.2
388 * @return string[]
389 */
390 function wc_get_chosen_shipping_method_ids() {
391 $method_ids = array();
392 $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
393 foreach ( $chosen_methods as $chosen_method ) {
394 $chosen_method = explode( ':', $chosen_method );
395 $method_ids[] = current( $chosen_method );
396 }
397 return $method_ids;
398 }
399
400 /**
401 * Get chosen method for package from session.
402 *
403 * @since 3.2.0
404 * @param int $key Key of package.
405 * @param array $package Package data array.
406 * @return string|bool
407 */
408 function wc_get_chosen_shipping_method_for_package( $key, $package ) {
409 $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
410 $chosen_method = isset( $chosen_methods[ $key ] ) ? $chosen_methods[ $key ] : false;
411 $changed = wc_shipping_methods_have_changed( $key, $package );
412
413 // This is deprecated but here for BW compat. TODO: Remove in 4.0.0.
414 $method_counts = WC()->session->get( 'shipping_method_counts' );
415
416 if ( ! empty( $method_counts[ $key ] ) ) {
417 $method_count = absint( $method_counts[ $key ] );
418 } else {
419 $method_count = 0;
420 }
421
422 // If not set, not available, or available methods have changed, set to the DEFAULT option.
423 if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) || count( $package['rates'] ) !== $method_count ) {
424 $chosen_method = wc_get_default_shipping_method_for_package( $key, $package, $chosen_method );
425 $chosen_methods[ $key ] = $chosen_method;
426 $method_counts[ $key ] = count( $package['rates'] );
427
428 WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
429 WC()->session->set( 'shipping_method_counts', $method_counts );
430
431 do_action( 'woocommerce_shipping_method_chosen', $chosen_method );
432 }
433 return $chosen_method;
434 }
435
436 /**
437 * Choose the default method for a package.
438 *
439 * @since 3.2.0
440 * @param int $key Key of package.
441 * @param array $package Package data array.
442 * @param string $chosen_method Chosen method id.
443 * @return string
444 */
445 function wc_get_default_shipping_method_for_package( $key, $package, $chosen_method ) {
446 $rate_keys = array_keys( $package['rates'] );
447 $default = current( $rate_keys );
448 $coupons = WC()->cart->get_coupons();
449 foreach ( $coupons as $coupon ) {
450 if ( $coupon->get_free_shipping() ) {
451 foreach ( $rate_keys as $rate_key ) {
452 if ( 0 === stripos( $rate_key, 'free_shipping' ) ) {
453 $default = $rate_key;
454 break;
455 }
456 }
457 break;
458 }
459 }
460 return apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method );
461 }
462
463 /**
464 * See if the methods have changed since the last request.
465 *
466 * @since 3.2.0
467 * @param int $key Key of package.
468 * @param array $package Package data array.
469 * @return bool
470 */
471 function wc_shipping_methods_have_changed( $key, $package ) {
472 // Lookup previous methods from session.
473 $previous_shipping_methods = WC()->session->get( 'previous_shipping_methods' );
474 // Get new and old rates.
475 $new_rates = array_keys( $package['rates'] );
476 $prev_rates = isset( $previous_shipping_methods[ $key ] ) ? $previous_shipping_methods[ $key ] : false;
477 // Update session.
478 $previous_shipping_methods[ $key ] = $new_rates;
479 WC()->session->set( 'previous_shipping_methods', $previous_shipping_methods );
480 return $new_rates !== $prev_rates;
481 }
482
483 /**
484 * Gets a hash of important product data that when changed should cause cart items to be invalidated.
485 *
486 * The woocommerce_cart_item_data_to_validate filter can be used to add custom properties.
487 *
488 * @param WC_Product $product Product object.
489 * @return string
490 */
491 function wc_get_cart_item_data_hash( $product ) {
492 return md5(
493 wp_json_encode(
494 apply_filters(
495 'woocommerce_cart_item_data_to_validate',
496 array(
497 'type' => $product->get_type(),
498 'attributes' => 'variation' === $product->get_type() ? $product->get_variation_attributes() : '',
499 ),
500 $product
501 )
502 )
503 );
504 }
505