abstracts
1 month ago
admin
6 days ago
blocks
11 months ago
cli
1 month ago
customizer
4 months ago
data-stores
1 month ago
emails
6 days ago
export
1 year ago
gateways
1 month ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
1 month ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
3 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
1 month ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 month ago
widgets
2 months ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months 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 month ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
1 month ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
1 day 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 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
3 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
6 days ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
1 month ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 month ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
1 month ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
1 month ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
1 month ago
class-wc-product-variation.php
1 month ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
7 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 month ago
class-woocommerce.php
1 day ago
wc-account-functions.php
1 month ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 month ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
1 month ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
1 month ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
1 month ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
1 month ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
6 days ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
1 month ago
wc-widget-functions.php
5 years ago
wc-cart-functions.php
623 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 ) . ' × ' : '' ), $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( '“%s”', '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 | |
| 181 | // If the order has been received, clear the cart. |
| 182 | if ( ! empty( $wp->query_vars['order-received'] ) ) { |
| 183 | |
| 184 | $order_id = absint( $wp->query_vars['order-received'] ); |
| 185 | $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok. |
| 186 | |
| 187 | if ( $order_id > 0 ) { |
| 188 | $order = wc_get_order( $order_id ); |
| 189 | |
| 190 | if ( $order instanceof WC_Order && hash_equals( $order->get_order_key(), $order_key ) ) { |
| 191 | $should_clear_cart_after_payment = true; |
| 192 | $after_payment = true; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // If the order is awaiting payment, and we haven't already decided to clear the cart, check the order status. |
| 198 | if ( is_object( WC()->session ) && WC()->session->order_awaiting_payment > 0 && ! $should_clear_cart_after_payment ) { |
| 199 | $order = wc_get_order( WC()->session->order_awaiting_payment ); |
| 200 | |
| 201 | if ( $order instanceof WC_Order && $order->get_id() > 0 ) { |
| 202 | // If the order status is neither pending, failed, nor cancelled, the order must have gone through. |
| 203 | $should_clear_cart_after_payment = ! $order->has_status( array( OrderStatus::FAILED, OrderStatus::PENDING, OrderStatus::CANCELLED ) ); |
| 204 | $after_payment = true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // If it doesn't look like a payment happened, bail early. |
| 209 | if ( ! $after_payment ) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Determine whether the cart should be cleared after payment. |
| 215 | * |
| 216 | * @since 9.3.0 |
| 217 | * @param bool $should_clear_cart_after_payment Whether the cart should be cleared after payment. |
| 218 | */ |
| 219 | $should_clear_cart_after_payment = apply_filters( 'woocommerce_should_clear_cart_after_payment', $should_clear_cart_after_payment ); |
| 220 | |
| 221 | if ( $should_clear_cart_after_payment ) { |
| 222 | WC()->cart->empty_cart(); |
| 223 | } |
| 224 | } |
| 225 | add_action( 'template_redirect', 'wc_clear_cart_after_payment', 20 ); |
| 226 | |
| 227 | /** |
| 228 | * Get the subtotal. |
| 229 | */ |
| 230 | function wc_cart_totals_subtotal_html() { |
| 231 | echo WC()->cart->get_cart_subtotal(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get shipping methods. |
| 236 | */ |
| 237 | function wc_cart_totals_shipping_html() { |
| 238 | $packages = WC()->shipping()->get_packages(); |
| 239 | $first = true; |
| 240 | |
| 241 | foreach ( $packages as $i => $package ) { |
| 242 | $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : ''; |
| 243 | $product_names = array(); |
| 244 | |
| 245 | if ( count( $packages ) > 1 ) { |
| 246 | foreach ( $package['contents'] as $item_id => $values ) { |
| 247 | $product_names[ $item_id ] = $values['data']->get_name() . ' ×' . $values['quantity']; |
| 248 | } |
| 249 | $product_names = apply_filters( 'woocommerce_shipping_package_details_array', $product_names, $package ); |
| 250 | } |
| 251 | |
| 252 | wc_get_template( |
| 253 | 'cart/cart-shipping.php', |
| 254 | array( |
| 255 | 'package' => $package, |
| 256 | 'available_methods' => $package['rates'], |
| 257 | 'show_package_details' => count( $packages ) > 1, |
| 258 | 'show_shipping_calculator' => is_cart() && apply_filters( 'woocommerce_shipping_show_shipping_calculator', $first, $i, $package ), |
| 259 | 'package_details' => implode( ', ', $product_names ), |
| 260 | 'package_name' => $package['package_name'], |
| 261 | 'index' => $i, |
| 262 | 'chosen_method' => $chosen_method, |
| 263 | 'formatted_destination' => WC()->countries->get_formatted_address( $package['destination'], ', ' ), |
| 264 | 'has_calculated_shipping' => WC()->customer->has_calculated_shipping(), |
| 265 | ) |
| 266 | ); |
| 267 | |
| 268 | $first = false; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get taxes total. |
| 274 | */ |
| 275 | function wc_cart_totals_taxes_total_html() { |
| 276 | echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Get a coupon label. |
| 281 | * |
| 282 | * @param string|WC_Coupon $coupon Coupon data or code. |
| 283 | * @param bool $echo Echo or return. |
| 284 | * |
| 285 | * @return string |
| 286 | */ |
| 287 | function wc_cart_totals_coupon_label( $coupon, $echo = true ) { |
| 288 | if ( is_string( $coupon ) ) { |
| 289 | $coupon = new WC_Coupon( $coupon ); |
| 290 | } |
| 291 | |
| 292 | /* translators: %s: coupon code */ |
| 293 | $label = apply_filters( 'woocommerce_cart_totals_coupon_label', sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ), $coupon->get_code() ), $coupon ); |
| 294 | |
| 295 | if ( $echo ) { |
| 296 | echo $label; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 297 | } else { |
| 298 | return $label; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get coupon display HTML. |
| 304 | * |
| 305 | * @param string|WC_Coupon $coupon Coupon data or code. |
| 306 | */ |
| 307 | function wc_cart_totals_coupon_html( $coupon ) { |
| 308 | if ( is_string( $coupon ) ) { |
| 309 | $coupon = new WC_Coupon( $coupon ); |
| 310 | } |
| 311 | |
| 312 | $amount = WC()->cart->get_coupon_discount_amount( $coupon->get_code(), WC()->cart->display_cart_ex_tax ); |
| 313 | $discount_amount_html = '-' . wc_price( $amount ); |
| 314 | |
| 315 | if ( $coupon->get_free_shipping() && empty( $amount ) ) { |
| 316 | $discount_amount_html = __( 'Free shipping coupon', 'woocommerce' ); |
| 317 | } |
| 318 | |
| 319 | $discount_amount_html = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_amount_html, $coupon ); |
| 320 | // translators: %s: coupon code. |
| 321 | $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>'; |
| 322 | |
| 323 | 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 |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Get order total html including inc tax if needed. |
| 328 | */ |
| 329 | function wc_cart_totals_order_total_html() { |
| 330 | $value = '<strong>' . WC()->cart->get_total() . '</strong> '; |
| 331 | |
| 332 | // If prices are tax inclusive, show taxes here. |
| 333 | if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) { |
| 334 | $tax_string_array = array(); |
| 335 | $cart_tax_totals = WC()->cart->get_tax_totals(); |
| 336 | |
| 337 | if ( get_option( 'woocommerce_tax_total_display' ) === 'itemized' ) { |
| 338 | foreach ( $cart_tax_totals as $code => $tax ) { |
| 339 | $tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label ); |
| 340 | } |
| 341 | } elseif ( ! empty( $cart_tax_totals ) ) { |
| 342 | $tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() ); |
| 343 | } |
| 344 | |
| 345 | if ( ! empty( $tax_string_array ) ) { |
| 346 | $taxable_address = WC()->customer->get_taxable_address(); |
| 347 | if ( WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() ) { |
| 348 | $country = WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ]; |
| 349 | /* translators: 1: tax amount 2: country name */ |
| 350 | $tax_text = wp_kses_post( sprintf( __( '(includes %1$s estimated for %2$s)', 'woocommerce' ), implode( ', ', $tax_string_array ), $country ) ); |
| 351 | } else { |
| 352 | /* translators: %s: tax amount */ |
| 353 | $tax_text = wp_kses_post( sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) ) ); |
| 354 | } |
| 355 | |
| 356 | $value .= '<small class="includes_tax">' . $tax_text . '</small>'; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get the fee value. |
| 365 | * |
| 366 | * @param object $fee Fee data. |
| 367 | */ |
| 368 | function wc_cart_totals_fee_html( $fee ) { |
| 369 | $cart_totals_fee_html = WC()->cart->display_prices_including_tax() ? wc_price( $fee->total + $fee->tax ) : wc_price( $fee->total ); |
| 370 | |
| 371 | echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get a shipping methods full label including price. |
| 376 | * |
| 377 | * @param WC_Shipping_Rate $method Shipping method rate data. |
| 378 | * @return string |
| 379 | */ |
| 380 | function wc_cart_totals_shipping_method_label( $method ) { |
| 381 | $label = $method->get_label(); |
| 382 | $has_cost = 0 < $method->cost; |
| 383 | $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true ); |
| 384 | |
| 385 | if ( $has_cost && ! $hide_cost ) { |
| 386 | if ( WC()->cart->display_prices_including_tax() ) { |
| 387 | $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() ); |
| 388 | if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) { |
| 389 | $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; |
| 390 | } |
| 391 | } else { |
| 392 | $label .= ': ' . wc_price( $method->cost ); |
| 393 | if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) { |
| 394 | $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method ); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Round discount. |
| 404 | * |
| 405 | * @param double $value Amount to round. |
| 406 | * @param int $precision DP to round. |
| 407 | * @return float |
| 408 | */ |
| 409 | function wc_cart_round_discount( $value, $precision ) { |
| 410 | return wc_round_discount( $value, $precision ); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Gets chosen shipping method IDs from chosen_shipping_methods session, without instance IDs. |
| 415 | * |
| 416 | * @since 2.6.2 |
| 417 | * @return string[] |
| 418 | */ |
| 419 | function wc_get_chosen_shipping_method_ids() { |
| 420 | if ( ! is_callable( array( WC()->session, 'get' ) ) ) { |
| 421 | return array(); |
| 422 | } |
| 423 | |
| 424 | $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() ); |
| 425 | $method_ids = array(); |
| 426 | |
| 427 | foreach ( $chosen_methods as $chosen_method ) { |
| 428 | if ( ! is_string( $chosen_method ) ) { |
| 429 | continue; |
| 430 | } |
| 431 | $chosen_method = explode( ':', $chosen_method ); |
| 432 | $method_ids[] = current( $chosen_method ); |
| 433 | } |
| 434 | |
| 435 | return $method_ids; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Get chosen method for package from session. |
| 440 | * |
| 441 | * @since 3.2.0 |
| 442 | * @param int $key Key of package. |
| 443 | * @param array $package Package data array. |
| 444 | * @return string|bool Either the chosen method ID or false if nothing is chosen yet. |
| 445 | */ |
| 446 | function wc_get_chosen_shipping_method_for_package( $key, $package ) { |
| 447 | if ( ! is_callable( array( WC()->session, 'get' ) ) ) { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() ); |
| 452 | $chosen_method = isset( $chosen_methods[ $key ] ) ? $chosen_methods[ $key ] : false; |
| 453 | $changed = wc_shipping_methods_have_changed( $key, $package ); |
| 454 | |
| 455 | // This is deprecated but here for BW compat. Remove in 4.0.0. |
| 456 | $method_counts = WC()->session->get( 'shipping_method_counts' ); |
| 457 | |
| 458 | if ( ! empty( $method_counts[ $key ] ) ) { |
| 459 | $method_count = absint( $method_counts[ $key ] ); |
| 460 | } else { |
| 461 | $method_count = 0; |
| 462 | } |
| 463 | |
| 464 | if ( ! isset( $package['rates'] ) || ! is_array( $package['rates'] ) ) { |
| 465 | $package['rates'] = array(); |
| 466 | } |
| 467 | |
| 468 | // If not set, not available, or available methods have changed, set to the DEFAULT option. |
| 469 | if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) || count( $package['rates'] ) !== $method_count ) { |
| 470 | $chosen_method = wc_get_default_shipping_method_for_package( $key, $package, $chosen_method ); |
| 471 | $chosen_methods[ $key ] = $chosen_method; |
| 472 | $method_counts[ $key ] = count( $package['rates'] ); |
| 473 | |
| 474 | WC()->session->set( 'chosen_shipping_methods', $chosen_methods ); |
| 475 | WC()->session->set( 'shipping_method_counts', $method_counts ); |
| 476 | |
| 477 | /** |
| 478 | * Fires when a shipping method is chosen. |
| 479 | * |
| 480 | * @since 3.2.0 |
| 481 | * @param string $chosen_method Chosen shipping method. e.g. flat_rate:1. |
| 482 | */ |
| 483 | do_action( 'woocommerce_shipping_method_chosen', $chosen_method ); |
| 484 | } |
| 485 | return $chosen_method; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Choose the default method for a package. |
| 490 | * |
| 491 | * @since 3.2.0 |
| 492 | * @param int $key Key of package. |
| 493 | * @param array $package Package data array. |
| 494 | * @param string $chosen_method Chosen shipping method. e.g. flat_rate:1. |
| 495 | * @return string |
| 496 | */ |
| 497 | function wc_get_default_shipping_method_for_package( $key, $package, $chosen_method ) { |
| 498 | $rate_keys = array_keys( $package['rates'] ); |
| 499 | $local_pickup_method_ids = LocalPickupUtils::get_local_pickup_method_ids(); |
| 500 | |
| 501 | if ( 'shortcode' === WC()->cart->cart_context ) { |
| 502 | $default = current( $rate_keys ); |
| 503 | } else { |
| 504 | // 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. |
| 505 | $default = CartCheckoutUtils::shipping_methods_exist() ? '' : current( $rate_keys ); |
| 506 | |
| 507 | // Default to the first method in the package that isn't a local pickup method. |
| 508 | foreach ( $rate_keys as $rate_key ) { |
| 509 | $rate_method_id = current( explode( ':', $rate_key ) ); |
| 510 | if ( ! in_array( $rate_method_id, $local_pickup_method_ids, true ) ) { |
| 511 | $default = $rate_key; |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // When shipping costs are hidden until an address is entered, don't auto-select pickup as the default. |
| 517 | // Without this, pickup gets silently selected because it's the only remaining rate after filtering. |
| 518 | if ( |
| 519 | '' === $default |
| 520 | && 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) |
| 521 | && WC()->customer instanceof \WC_Customer |
| 522 | && ! WC()->customer->has_full_shipping_address() |
| 523 | ) { |
| 524 | /** |
| 525 | * Filters the default shipping method for a package. |
| 526 | * |
| 527 | * @since 3.2.0 |
| 528 | * @param string $default Default shipping method. |
| 529 | * @param array $rates Shipping rates. |
| 530 | * @param string $chosen_method Chosen method id. |
| 531 | */ |
| 532 | return (string) apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method ); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * If the customer has selected local pickup, keep it selected if it's still in the package. We don't want to auto |
| 538 | * toggle between shipping and pickup even if available shipping methods are changed. |
| 539 | * |
| 540 | * This is important for block-based checkout where there is an explicit toggle between shipping and pickup. |
| 541 | */ |
| 542 | $chosen_method_id = current( explode( ':', $chosen_method ) ); |
| 543 | $chosen_method_exists = in_array( $chosen_method, $rate_keys, true ); |
| 544 | $is_local_pickup_chosen = in_array( $chosen_method_id, $local_pickup_method_ids, true ); |
| 545 | |
| 546 | // Default to local pickup if its chosen already. |
| 547 | if ( $chosen_method_exists && $is_local_pickup_chosen ) { |
| 548 | $default = $chosen_method; |
| 549 | |
| 550 | } else { |
| 551 | // Check coupons to see if free shipping is available. If it is, we'll use that method as the default. |
| 552 | $coupons = WC()->cart->get_coupons(); |
| 553 | foreach ( $coupons as $coupon ) { |
| 554 | if ( $coupon->get_free_shipping() ) { |
| 555 | foreach ( $rate_keys as $rate_key ) { |
| 556 | if ( 0 === stripos( $rate_key, 'free_shipping' ) ) { |
| 557 | $default = $rate_key; |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Filters the default shipping method for a package. |
| 568 | * |
| 569 | * @since 3.2.0 |
| 570 | * @param string $default Default shipping method. |
| 571 | * @param array $rates Shipping rates. |
| 572 | * @param string $chosen_method Chosen method id. |
| 573 | */ |
| 574 | return (string) apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method ); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * See if the methods have changed since the last request. |
| 579 | * |
| 580 | * @since 3.2.0 |
| 581 | * @param int $key Key of package. |
| 582 | * @param array $package Package data array. |
| 583 | * @return bool |
| 584 | */ |
| 585 | function wc_shipping_methods_have_changed( $key, $package ) { |
| 586 | if ( ! is_callable( array( WC()->session, 'get' ) ) ) { |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | // Lookup previous methods from session. |
| 591 | $previous_shipping_methods = WC()->session->get( 'previous_shipping_methods' ); |
| 592 | // Get new and old rates. |
| 593 | $new_rates = array_keys( $package['rates'] ); |
| 594 | $prev_rates = isset( $previous_shipping_methods[ $key ] ) ? $previous_shipping_methods[ $key ] : false; |
| 595 | // Update session. |
| 596 | $previous_shipping_methods[ $key ] = $new_rates; |
| 597 | WC()->session->set( 'previous_shipping_methods', $previous_shipping_methods ); |
| 598 | return $new_rates !== $prev_rates; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Gets a hash of important product data that when changed should cause cart items to be invalidated. |
| 603 | * |
| 604 | * The woocommerce_cart_item_data_to_validate filter can be used to add custom properties. |
| 605 | * |
| 606 | * @param WC_Product $product Product object. |
| 607 | * @return string |
| 608 | */ |
| 609 | function wc_get_cart_item_data_hash( $product ) { |
| 610 | return md5( |
| 611 | wp_json_encode( |
| 612 | apply_filters( |
| 613 | 'woocommerce_cart_item_data_to_validate', |
| 614 | array( |
| 615 | 'type' => $product->get_type(), |
| 616 | 'attributes' => ProductType::VARIATION === $product->get_type() ? $product->get_variation_attributes() : '', |
| 617 | ), |
| 618 | $product |
| 619 | ) |
| 620 | ) |
| 621 | ); |
| 622 | } |
| 623 |