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