暫無描述

wc-cart-functions.php 17KB

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