WC_Cart

WooCommerce cart.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-cart.php  
  1. class WC_Cart { 
  2.  
  3. /** @var array Contains an array of cart items. */ 
  4. public $cart_contents = array(); 
  5.  
  6. /** @var array Contains an array of removed cart items. */ 
  7. public $removed_cart_contents = array(); 
  8.  
  9. /** @var array Contains an array of coupon codes applied to the cart. */ 
  10. public $applied_coupons = array(); 
  11.  
  12. /** @var array Contains an array of coupon code discounts after they have been applied. */ 
  13. public $coupon_discount_amounts = array(); 
  14.  
  15. /** @var array Contains an array of coupon code discount taxes. Used for tax incl pricing. */ 
  16. public $coupon_discount_tax_amounts = array(); 
  17.  
  18. /** @var array Contains an array of coupon usage counts after they have been applied. */ 
  19. public $coupon_applied_count = array(); 
  20.  
  21. /** @var array Array of coupons */ 
  22. public $coupons = array(); 
  23.  
  24. /** @var float The total cost of the cart items. */ 
  25. public $cart_contents_total; 
  26.  
  27. /** @var float Cart grand total. */ 
  28. public $total; 
  29.  
  30. /** @var float Cart subtotal. */ 
  31. public $subtotal; 
  32.  
  33. /** @var float Cart subtotal without tax. */ 
  34. public $subtotal_ex_tax; 
  35.  
  36. /** @var float Total cart tax. */ 
  37. public $tax_total; 
  38.  
  39. /** @var array An array of taxes/tax rates for the cart. */ 
  40. public $taxes; 
  41.  
  42. /** @var array An array of taxes/tax rates for the shipping. */ 
  43. public $shipping_taxes; 
  44.  
  45. /** @var float Discount amount before tax */ 
  46. public $discount_cart; 
  47.  
  48. /** @var float Discounted tax amount. Used predominantly for displaying tax inclusive prices correctly */ 
  49. public $discount_cart_tax; 
  50.  
  51. /** @var float Total for additional fees. */ 
  52. public $fee_total; 
  53.  
  54. /** @var float Shipping cost. */ 
  55. public $shipping_total; 
  56.  
  57. /** @var float Shipping tax. */ 
  58. public $shipping_tax_total; 
  59.  
  60. /** @var array cart_session_data. Array of data the cart calculates and stores in the session with defaults */ 
  61. public $cart_session_data = array( 
  62. 'cart_contents_total' => 0,  
  63. 'total' => 0,  
  64. 'subtotal' => 0,  
  65. 'subtotal_ex_tax' => 0,  
  66. 'tax_total' => 0,  
  67. 'taxes' => array(),  
  68. 'shipping_taxes' => array(),  
  69. 'discount_cart' => 0,  
  70. 'discount_cart_tax' => 0,  
  71. 'shipping_total' => 0,  
  72. 'shipping_tax_total' => 0,  
  73. 'coupon_discount_amounts' => array(),  
  74. 'coupon_discount_tax_amounts' => array(),  
  75. 'fee_total' => 0,  
  76. 'fees' => array(),  
  77. ); 
  78.  
  79. /** 
  80. * An array of fees. 
  81. * @var array 
  82. */ 
  83. public $fees = array(); 
  84.  
  85. /** 
  86. * Constructor for the cart class. Loads options and hooks in the init method. 
  87. */ 
  88. public function __construct() { 
  89. add_action( 'wp_loaded', array( $this, 'init' ) ); // Get cart after WP and plugins are loaded. 
  90. add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); // Set cookies 
  91. add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing 
  92. add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 ); 
  93. add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 ); 
  94.  
  95. /** 
  96. * Auto-load in-accessible properties on demand. 
  97. * @param mixed $key 
  98. * @return mixed 
  99. */ 
  100. public function __get( $key ) { 
  101. switch ( $key ) { 
  102. case 'prices_include_tax' : 
  103. return wc_prices_include_tax(); 
  104. break; 
  105. case 'round_at_subtotal' : 
  106. return 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ); 
  107. break; 
  108. case 'tax_display_cart' : 
  109. return get_option( 'woocommerce_tax_display_cart' ); 
  110. break; 
  111. case 'dp' : 
  112. return wc_get_price_decimals(); 
  113. break; 
  114. case 'display_totals_ex_tax' : 
  115. case 'display_cart_ex_tax' : 
  116. return 'excl' === $this->tax_display_cart; 
  117. break; 
  118. case 'cart_contents_weight' : 
  119. return $this->get_cart_contents_weight(); 
  120. break; 
  121. case 'cart_contents_count' : 
  122. return $this->get_cart_contents_count(); 
  123. break; 
  124. case 'tax' : 
  125. wc_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax:: directly' ); 
  126. $this->tax = new WC_Tax(); 
  127. return $this->tax; 
  128. case 'discount_total': 
  129. wc_deprecated_argument( 'WC_Cart->discount_total', '2.3', 'After tax coupons are no longer supported. For more information see: https://woocommerce.wordpress.com/2014/12/upcoming-coupon-changes-in-woocommerce-2-3/' ); 
  130. return 0; 
  131.  
  132. /** 
  133. * Loads the cart data from the PHP session during WordPress init and hooks in other methods. 
  134. */ 
  135. public function init() { 
  136. $this->get_cart_from_session(); 
  137.  
  138. add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 ); 
  139. add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 ); 
  140. add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 ); 
  141.  
  142. /** 
  143. * Will set cart cookies if needed, once, during WP hook. 
  144. */ 
  145. public function maybe_set_cart_cookies() { 
  146. if ( ! headers_sent() && did_action( 'wp_loaded' ) ) { 
  147. if ( ! $this->is_empty() ) { 
  148. $this->set_cart_cookies( true ); 
  149. } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { 
  150. $this->set_cart_cookies( false ); 
  151.  
  152. /** 
  153. * Set cart hash cookie and items in cart. 
  154. * @access private 
  155. * @param bool $set (default: true) 
  156. */ 
  157. private function set_cart_cookies( $set = true ) { 
  158. if ( $set ) { 
  159. wc_setcookie( 'woocommerce_items_in_cart', 1 ); 
  160. wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart_for_session() ) ) ); 
  161. } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { 
  162. wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS ); 
  163. wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS ); 
  164. do_action( 'woocommerce_set_cart_cookies', $set ); 
  165.  
  166. /** 
  167. /** Cart Session Handling 
  168. */ 
  169.  
  170. /** 
  171. * Get the cart data from the PHP session and store it in class variables. 
  172. */ 
  173. public function get_cart_from_session() { 
  174. // Load cart session data from session 
  175. foreach ( $this->cart_session_data as $key => $default ) { 
  176. $this->$key = WC()->session->get( $key, $default ); 
  177.  
  178. $update_cart_session = false; 
  179. $this->removed_cart_contents = array_filter( WC()->session->get( 'removed_cart_contents', array() ) ); 
  180. $this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) ); 
  181.  
  182. /** 
  183. * Load the cart object. This defaults to the persistent cart if null. 
  184. */ 
  185. $cart = WC()->session->get( 'cart', null ); 
  186.  
  187. if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', true ) ) ) { 
  188. $cart = $saved_cart['cart']; 
  189. $update_cart_session = true; 
  190. } elseif ( is_null( $cart ) ) { 
  191. $cart = array(); 
  192.  
  193. if ( is_array( $cart ) ) { 
  194. // Prime meta cache to reduce future queries 
  195. update_meta_cache( 'post', wp_list_pluck( $cart, 'product_id' ) ); 
  196. update_object_term_cache( wp_list_pluck( $cart, 'product_id' ), 'product' ); 
  197.  
  198. foreach ( $cart as $key => $values ) { 
  199. $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] ); 
  200.  
  201. if ( ! empty( $product ) && $product->exists() && $values['quantity'] > 0 ) { 
  202.  
  203. if ( ! $product->is_purchasable() ) { 
  204.  
  205. // Flag to indicate the stored cart should be update 
  206. $update_cart_session = true; 
  207. /** translators: %s: product name */ 
  208. wc_add_notice( sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $product->get_name() ), 'error' ); 
  209. do_action( 'woocommerce_remove_cart_item_from_session', $key, $values ); 
  210.  
  211. } else { 
  212.  
  213. // Put session data into array. Run through filter so other plugins can load their own session data 
  214. $session_data = array_merge( $values, array( 'data' => $product ) ); 
  215. $this->cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key ); 
  216.  
  217.  
  218. // Trigger action 
  219. do_action( 'woocommerce_cart_loaded_from_session', $this ); 
  220.  
  221. if ( $update_cart_session ) { 
  222. WC()->session->cart = $this->get_cart_for_session(); 
  223.  
  224. // Queue re-calc if subtotal is not set 
  225. if ( ( ! $this->subtotal && ! $this->is_empty() ) || $update_cart_session ) { 
  226. $this->calculate_totals(); 
  227.  
  228. /** 
  229. * Sets the php session data for the cart and coupons. 
  230. */ 
  231. public function set_session() { 
  232. // Set cart and coupon session data 
  233. $cart_session = $this->get_cart_for_session(); 
  234.  
  235. WC()->session->set( 'cart', $cart_session ); 
  236. WC()->session->set( 'applied_coupons', $this->applied_coupons ); 
  237. WC()->session->set( 'coupon_discount_amounts', $this->coupon_discount_amounts ); 
  238. WC()->session->set( 'coupon_discount_tax_amounts', $this->coupon_discount_tax_amounts ); 
  239. WC()->session->set( 'removed_cart_contents', $this->removed_cart_contents ); 
  240.  
  241. foreach ( $this->cart_session_data as $key => $default ) { 
  242. WC()->session->set( $key, $this->$key ); 
  243.  
  244. if ( get_current_user_id() ) { 
  245. $this->persistent_cart_update(); 
  246.  
  247. do_action( 'woocommerce_cart_updated' ); 
  248.  
  249. /** 
  250. * Empties the cart and optionally the persistent cart too. 
  251. * @param bool $clear_persistent_cart (default: true) 
  252. */ 
  253. public function empty_cart( $clear_persistent_cart = true ) { 
  254. $this->cart_contents = array(); 
  255. $this->reset( true ); 
  256.  
  257. unset( WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart ); 
  258.  
  259. if ( $clear_persistent_cart && get_current_user_id() ) { 
  260. $this->persistent_cart_destroy(); 
  261.  
  262. do_action( 'woocommerce_cart_emptied' ); 
  263.  
  264. /** 
  265. * Persistent cart handling 
  266. */ 
  267.  
  268. /** 
  269. * Save the persistent cart when the cart is updated. 
  270. */ 
  271. public function persistent_cart_update() { 
  272. update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', array( 
  273. 'cart' => WC()->session->get( 'cart' ),  
  274. ) ); 
  275.  
  276. /** 
  277. * Delete the persistent cart permanently. 
  278. */ 
  279. public function persistent_cart_destroy() { 
  280. delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart' ); 
  281.  
  282. /** 
  283. * Cart Data Functions. 
  284. */ 
  285.  
  286. /** 
  287. * Coupons enabled function. Filterable. 
  288. * @deprecated 2.5.0 in favor to wc_coupons_enabled() 
  289. * @return bool 
  290. */ 
  291. public function coupons_enabled() { 
  292. return wc_coupons_enabled(); 
  293.  
  294. /** 
  295. * Get number of items in the cart. 
  296. * @return int 
  297. */ 
  298. public function get_cart_contents_count() { 
  299. return apply_filters( 'woocommerce_cart_contents_count', array_sum( wp_list_pluck( $this->get_cart(), 'quantity' ) ) ); 
  300.  
  301. /** 
  302. * Get weight of items in the cart. 
  303. * @since 2.5.0 
  304. * @return int 
  305. */ 
  306. public function get_cart_contents_weight() { 
  307. $weight = 0; 
  308.  
  309. foreach ( $this->get_cart() as $cart_item_key => $values ) { 
  310. $weight += (float) $values['data']->get_weight() * $values['quantity']; 
  311.  
  312. return apply_filters( 'woocommerce_cart_contents_weight', $weight ); 
  313.  
  314. /** 
  315. * Checks if the cart is empty. 
  316. * @return bool 
  317. */ 
  318. public function is_empty() { 
  319. return 0 === sizeof( $this->get_cart() ); 
  320.  
  321. /** 
  322. * Check all cart items for errors. 
  323. */ 
  324. public function check_cart_items() { 
  325.  
  326. // Result 
  327. $return = true; 
  328.  
  329. // Check cart item validity 
  330. $result = $this->check_cart_item_validity(); 
  331.  
  332. if ( is_wp_error( $result ) ) { 
  333. wc_add_notice( $result->get_error_message(), 'error' ); 
  334. $return = false; 
  335.  
  336. // Check item stock 
  337. $result = $this->check_cart_item_stock(); 
  338.  
  339. if ( is_wp_error( $result ) ) { 
  340. wc_add_notice( $result->get_error_message(), 'error' ); 
  341. $return = false; 
  342.  
  343. return $return; 
  344.  
  345.  
  346. /** 
  347. * Check cart coupons for errors. 
  348. */ 
  349. public function check_cart_coupons() { 
  350. foreach ( $this->applied_coupons as $code ) { 
  351. $coupon = new WC_Coupon( $code ); 
  352.  
  353. if ( ! $coupon->is_valid() ) { 
  354. // Error message 
  355. $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED ); 
  356.  
  357. // Remove the coupon 
  358. $this->remove_coupon( $code ); 
  359.  
  360. // Flag totals for refresh 
  361. WC()->session->set( 'refresh_totals', true ); 
  362.  
  363. /** 
  364. * Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines. 
  365. * @return array 
  366. */ 
  367. public function get_cart_item_quantities() { 
  368. $quantities = array(); 
  369.  
  370. foreach ( $this->get_cart() as $cart_item_key => $values ) { 
  371. $product = $values['data']; 
  372. $quantities[ $product->get_stock_managed_by_id() ] = isset( $quantities[ $product->get_stock_managed_by_id() ] ) ? $quantities[ $product->get_stock_managed_by_id() ] + $values['quantity'] : $values['quantity']; 
  373.  
  374. return $quantities; 
  375.  
  376. /** 
  377. * Looks through cart items and checks the posts are not trashed or deleted. 
  378. * @return bool|WP_Error 
  379. */ 
  380. public function check_cart_item_validity() { 
  381. $return = true; 
  382.  
  383. foreach ( $this->get_cart() as $cart_item_key => $values ) { 
  384. $product = $values['data']; 
  385.  
  386. if ( ! $product || ! $product->exists() || 'trash' === $product->get_status() ) { 
  387. $this->set_quantity( $cart_item_key, 0 ); 
  388. $return = new WP_Error( 'invalid', __( 'An item which is no longer available was removed from your cart.', 'woocommerce' ) ); 
  389.  
  390. return $return; 
  391.  
  392. /** 
  393. * Looks through the cart to check each item is in stock. If not, add an error. 
  394. * @return bool|WP_Error 
  395. */ 
  396. public function check_cart_item_stock() { 
  397. global $wpdb; 
  398.  
  399. $error = new WP_Error(); 
  400. $product_qty_in_cart = $this->get_cart_item_quantities(); 
  401.  
  402. // First stock check loop 
  403. foreach ( $this->get_cart() as $cart_item_key => $values ) { 
  404. $product = $values['data']; 
  405.  
  406. /** 
  407. * Check stock based on stock-status. 
  408. */ 
  409. if ( ! $product->is_in_stock() ) { 
  410. /** translators: %s: product name */ 
  411. $error->add( 'out-of-stock', sprintf( __( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name() ) ); 
  412. return $error; 
  413.  
  414. if ( ! $product->managing_stock() ) { 
  415. continue; 
  416.  
  417. /** 
  418. * Check stock based on all items in the cart. 
  419. */ 
  420. if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) { 
  421. /** translators: 1: product name 2: quantity in stock */ 
  422. $error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity(), $product ) ) ); 
  423. return $error; 
  424.  
  425. /** 
  426. * Finally consider any held stock, from pending orders. 
  427. */ 
  428. if ( get_option( 'woocommerce_hold_stock_minutes' ) > 0 && ! $product->backorders_allowed() ) { 
  429. $order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : 0; 
  430. $held_stock = $wpdb->get_var( 
  431. $wpdb->prepare( " 
  432. SELECT SUM( order_item_meta.meta_value ) AS held_qty 
  433. FROM {$wpdb->posts} AS posts 
  434. LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id 
  435. LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id 
  436. LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id 
  437. WHERE order_item_meta.meta_key = '_qty' 
  438. AND order_item_meta2.meta_key = %s AND order_item_meta2.meta_value = %d 
  439. AND posts.post_type IN ( '" . implode( "', '", wc_get_order_types() ) . "' ) 
  440. AND posts.post_status = 'wc-pending' 
  441. AND posts.ID != %d;",  
  442. 'variation' === get_post_type( $product->get_stock_managed_by_id() ) ? '_variation_id' : '_product_id',  
  443. $product->get_stock_managed_by_id(),  
  444. $order_id 
  445. ); 
  446.  
  447. if ( $product->get_stock_quantity() < ( $held_stock + $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) { 
  448. /** translators: 1: product name 2: minutes */ 
  449. $error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order right now. Please try again in %2$d minutes or edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), get_option( 'woocommerce_hold_stock_minutes' ) ) ); 
  450. return $error; 
  451.  
  452. return true; 
  453.  
  454. /** 
  455. * Gets and formats a list of cart item data + variations for display on the frontend. 
  456. * @param array $cart_item 
  457. * @param bool $flat (default: false) 
  458. * @return string 
  459. */ 
  460. public function get_item_data( $cart_item, $flat = false ) { 
  461. $item_data = array(); 
  462.  
  463. // Variation values are shown only if they are not found in the title as of 3.0. 
  464. // This is because variation titles display the attributes. 
  465. if ( $cart_item['data']->is_type( 'variation' ) && is_array( $cart_item['variation'] ) ) { 
  466. foreach ( $cart_item['variation'] as $name => $value ) { 
  467. $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) ); 
  468.  
  469. // If this is a term slug, get the term's nice name 
  470. if ( taxonomy_exists( $taxonomy ) ) { 
  471. $term = get_term_by( 'slug', $value, $taxonomy ); 
  472. if ( ! is_wp_error( $term ) && $term && $term->name ) { 
  473. $value = $term->name; 
  474. $label = wc_attribute_label( $taxonomy ); 
  475.  
  476. // If this is a custom option slug, get the options name. 
  477. } else { 
  478. $value = apply_filters( 'woocommerce_variation_option_name', $value ); 
  479. $label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $cart_item['data'] ); 
  480.  
  481. // Check the nicename against the title. 
  482. if ( '' === $value || wc_is_attribute_in_product_name( $value, $cart_item['data']->get_name() ) ) { 
  483. continue; 
  484.  
  485. $item_data[] = array( 
  486. 'key' => $label,  
  487. 'value' => $value,  
  488. ); 
  489.  
  490. // Filter item data to allow 3rd parties to add more to the array 
  491. $item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $cart_item ); 
  492.  
  493. // Format item data ready to display 
  494. foreach ( $item_data as $key => $data ) { 
  495. // Set hidden to true to not display meta on cart. 
  496. if ( ! empty( $data['hidden'] ) ) { 
  497. unset( $item_data[ $key ] ); 
  498. continue; 
  499. $item_data[ $key ]['key'] = ! empty( $data['key'] ) ? $data['key'] : $data['name']; 
  500. $item_data[ $key ]['display'] = ! empty( $data['display'] ) ? $data['display'] : $data['value']; 
  501.  
  502. // Output flat or in list format 
  503. if ( sizeof( $item_data ) > 0 ) { 
  504. ob_start(); 
  505.  
  506. if ( $flat ) { 
  507. foreach ( $item_data as $data ) { 
  508. echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['display'] ) . "\n"; 
  509. } else { 
  510. wc_get_template( 'cart/cart-item-data.php', array( 'item_data' => $item_data ) ); 
  511.  
  512. return ob_get_clean(); 
  513.  
  514. return ''; 
  515.  
  516. /** 
  517. * Gets cross sells based on the items in the cart. 
  518. * @return array cross_sells (item ids) 
  519. */ 
  520. public function get_cross_sells() { 
  521. $cross_sells = array(); 
  522. $in_cart = array(); 
  523. if ( ! $this->is_empty() ) { 
  524. foreach ( $this->get_cart() as $cart_item_key => $values ) { 
  525. if ( $values['quantity'] > 0 ) { 
  526. $cross_sells = array_merge( $values['data']->get_cross_sell_ids(), $cross_sells ); 
  527. $in_cart[] = $values['product_id']; 
  528. $cross_sells = array_diff( $cross_sells, $in_cart ); 
  529. return wp_parse_id_list( $cross_sells ); 
  530.  
  531. /** 
  532. * Gets the url to the cart page. 
  533. * @deprecated 2.5.0 in favor to wc_get_cart_url() 
  534. * @return string url to page 
  535. */ 
  536. public function get_cart_url() { 
  537. return wc_get_cart_url(); 
  538.  
  539. /** 
  540. * Gets the url to the checkout page. 
  541. * @deprecated 2.5.0 in favor to wc_get_checkout_url() 
  542. * @return string url to page 
  543. */ 
  544. public function get_checkout_url() { 
  545. return wc_get_checkout_url(); 
  546.  
  547. /** 
  548. * Gets the url to remove an item from the cart. 
  549. * @param string $cart_item_key contains the id of the cart item 
  550. * @return string url to page 
  551. */ 
  552. public function get_remove_url( $cart_item_key ) { 
  553. $cart_page_url = wc_get_page_permalink( 'cart' ); 
  554. return apply_filters( 'woocommerce_get_remove_url', $cart_page_url ? wp_nonce_url( add_query_arg( 'remove_item', $cart_item_key, $cart_page_url ), 'woocommerce-cart' ) : '' ); 
  555.  
  556. /** 
  557. * Gets the url to re-add an item into the cart. 
  558. * @param string $cart_item_key 
  559. * @return string url to page 
  560. */ 
  561. public function get_undo_url( $cart_item_key ) { 
  562. $cart_page_url = wc_get_page_permalink( 'cart' ); 
  563.  
  564. $query_args = array( 
  565. 'undo_item' => $cart_item_key,  
  566. ); 
  567.  
  568. return apply_filters( 'woocommerce_get_undo_url', $cart_page_url ? wp_nonce_url( add_query_arg( $query_args, $cart_page_url ), 'woocommerce-cart' ) : '', $cart_item_key ); 
  569.  
  570. /** 
  571. * Returns the contents of the cart in an array. 
  572. * @return array contents of the cart 
  573. */ 
  574. public function get_cart() { 
  575. if ( ! did_action( 'wp_loaded' ) ) { 
  576. wc_doing_it_wrong( __FUNCTION__, __( 'Get cart should not be called before the wp_loaded action.', 'woocommerce' ), '2.3' ); 
  577. if ( ! did_action( 'woocommerce_cart_loaded_from_session' ) ) { 
  578. $this->get_cart_from_session(); 
  579. return array_filter( (array) $this->cart_contents ); 
  580.  
  581. /** 
  582. * Returns the contents of the cart in an array without the 'data' element. 
  583. * @return array contents of the cart 
  584. */ 
  585. public function get_cart_for_session() { 
  586. $cart_session = array(); 
  587.  
  588. if ( $this->get_cart() ) { 
  589. foreach ( $this->get_cart() as $key => $values ) { 
  590. $cart_session[ $key ] = $values; 
  591. unset( $cart_session[ $key ]['data'] ); // Unset product object 
  592.  
  593. return $cart_session; 
  594.  
  595. /** 
  596. * Returns a specific item in the cart. 
  597. * @param string $item_key Cart item key. 
  598. * @return array Item data 
  599. */ 
  600. public function get_cart_item( $item_key ) { 
  601. if ( isset( $this->cart_contents[ $item_key ] ) ) { 
  602. return $this->cart_contents[ $item_key ]; 
  603.  
  604. return array(); 
  605.  
  606. /** 
  607. * Returns the cart and shipping taxes, merged. 
  608. * @return array merged taxes 
  609. */ 
  610. public function get_taxes() { 
  611. $taxes = array(); 
  612.  
  613. // Merge 
  614. foreach ( array_keys( $this->taxes + $this->shipping_taxes ) as $key ) { 
  615. $taxes[ $key ] = ( isset( $this->shipping_taxes[ $key ] ) ? $this->shipping_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); 
  616.  
  617. return apply_filters( 'woocommerce_cart_get_taxes', $taxes, $this ); 
  618.  
  619. /** 
  620. * Get taxes, merged by code, formatted ready for output. 
  621. * @return array 
  622. */ 
  623. public function get_tax_totals() { 
  624. $taxes = $this->get_taxes(); 
  625. $tax_totals = array(); 
  626.  
  627. foreach ( $taxes as $key => $tax ) { 
  628. $code = WC_Tax::get_rate_code( $key ); 
  629.  
  630. if ( $code || apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) === $key ) { 
  631. if ( ! isset( $tax_totals[ $code ] ) ) { 
  632. $tax_totals[ $code ] = new stdClass(); 
  633. $tax_totals[ $code ]->amount = 0; 
  634. $tax_totals[ $code ]->tax_rate_id = $key; 
  635. $tax_totals[ $code ]->is_compound = WC_Tax::is_compound( $key ); 
  636. $tax_totals[ $code ]->label = WC_Tax::get_rate_label( $key ); 
  637. $tax_totals[ $code ]->amount += wc_round_tax_total( $tax ); 
  638. $tax_totals[ $code ]->formatted_amount = wc_price( wc_round_tax_total( $tax_totals[ $code ]->amount ) ); 
  639.  
  640. if ( apply_filters( 'woocommerce_cart_hide_zero_taxes', true ) ) { 
  641. $amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) ); 
  642. $tax_totals = array_intersect_key( $tax_totals, $amounts ); 
  643.  
  644. return apply_filters( 'woocommerce_cart_tax_totals', $tax_totals, $this ); 
  645.  
  646. /** 
  647. * Get all tax classes for items in the cart. 
  648. * @return array 
  649. */ 
  650. public function get_cart_item_tax_classes() { 
  651. $found_tax_classes = array(); 
  652.  
  653. foreach ( WC()->cart->get_cart() as $item ) { 
  654. $found_tax_classes[] = $item['data']->get_tax_class(); 
  655.  
  656. return array_unique( $found_tax_classes ); 
  657.  
  658. /** 
  659. * Determines the value that the customer spent and the subtotal 
  660. * displayed, used for things like coupon validation. 
  661. * Since the coupon lines are displayed based on the TAX DISPLAY value 
  662. * of cart, this is used to determine the spend. 
  663. * If cart totals are shown including tax, use the subtotal. 
  664. * If cart totals are shown excluding tax, use the subtotal ex tax 
  665. * (tax is shown after coupons). 
  666. * @since 2.6.0 
  667. * @return string 
  668. */ 
  669. public function get_displayed_subtotal() { 
  670. if ( 'incl' === $this->tax_display_cart ) { 
  671. return wc_format_decimal( $this->subtotal ); 
  672. } elseif ( 'excl' === $this->tax_display_cart ) { 
  673. return wc_format_decimal( $this->subtotal_ex_tax ); 
  674.  
  675. /** 
  676. * Add to cart handling. 
  677. */ 
  678.  
  679. /** 
  680. * Check if product is in the cart and return cart item key. 
  681. * Cart item key will be unique based on the item and its properties, such as variations. 
  682. * @param mixed id of product to find in the cart 
  683. * @return string cart item key 
  684. */ 
  685. public function find_product_in_cart( $cart_id = false ) { 
  686. if ( false !== $cart_id ) { 
  687. if ( is_array( $this->cart_contents ) && isset( $this->cart_contents[ $cart_id ] ) ) { 
  688. return $cart_id; 
  689. return ''; 
  690.  
  691. /** 
  692. * Generate a unique ID for the cart item being added. 
  693. * @param int $product_id - id of the product the key is being generated for 
  694. * @param int $variation_id of the product the key is being generated for 
  695. * @param array $variation data for the cart item 
  696. * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart 
  697. * @return string cart item key 
  698. */ 
  699. public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { 
  700. $id_parts = array( $product_id ); 
  701.  
  702. if ( $variation_id && 0 != $variation_id ) { 
  703. $id_parts[] = $variation_id; 
  704.  
  705. if ( is_array( $variation ) && ! empty( $variation ) ) { 
  706. $variation_key = ''; 
  707. foreach ( $variation as $key => $value ) { 
  708. $variation_key .= trim( $key ) . trim( $value ); 
  709. $id_parts[] = $variation_key; 
  710.  
  711. if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) { 
  712. $cart_item_data_key = ''; 
  713. foreach ( $cart_item_data as $key => $value ) { 
  714. if ( is_array( $value ) || is_object( $value ) ) { 
  715. $value = http_build_query( $value ); 
  716. $cart_item_data_key .= trim( $key ) . trim( $value ); 
  717.  
  718. $id_parts[] = $cart_item_data_key; 
  719.  
  720. return apply_filters( 'woocommerce_cart_id', md5( implode( '_', $id_parts ) ), $product_id, $variation_id, $variation, $cart_item_data ); 
  721.  
  722. /** 
  723. * Add a product to the cart. 
  724. * @param int $product_id contains the id of the product to add to the cart 
  725. * @param int $quantity contains the quantity of the item to add 
  726. * @param int $variation_id 
  727. * @param array $variation attribute values 
  728. * @param array $cart_item_data extra cart item data we want to pass into the item 
  729. * @return string|bool $cart_item_key 
  730. */ 
  731. public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { 
  732. // Wrap in try catch so plugins can throw an exception to prevent adding to cart 
  733. try { 
  734. $product_id = absint( $product_id ); 
  735. $variation_id = absint( $variation_id ); 
  736.  
  737. // Ensure we don't add a variation to the cart directly by variation ID 
  738. if ( 'product_variation' === get_post_type( $product_id ) ) { 
  739. $variation_id = $product_id; 
  740. $product_id = wp_get_post_parent_id( $variation_id ); 
  741.  
  742. // Get the product 
  743. $product_data = wc_get_product( $variation_id ? $variation_id : $product_id ); 
  744.  
  745. // Sanity check 
  746. if ( $quantity <= 0 || ! $product_data || 'trash' === $product_data->get_status() ) { 
  747. return false; 
  748.  
  749. // Load cart item data - may be added by other plugins 
  750. $cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id ); 
  751.  
  752. // Generate a ID based on product ID, variation ID, variation data, and other cart item data 
  753. $cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data ); 
  754.  
  755. // Find the cart item key in the existing cart 
  756. $cart_item_key = $this->find_product_in_cart( $cart_id ); 
  757.  
  758. // Force quantity to 1 if sold individually and check for existing item in cart 
  759. if ( $product_data->is_sold_individually() ) { 
  760. $quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data ); 
  761. $in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0; 
  762.  
  763. if ( $in_cart_quantity > 0 ) { 
  764. /** translators: %s: product name */ 
  765. throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View cart', 'woocommerce' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_name() ) ) ); 
  766.  
  767. // Check product is_purchasable 
  768. if ( ! $product_data->is_purchasable() ) { 
  769. throw new Exception( __( 'Sorry, this product cannot be purchased.', 'woocommerce' ) ); 
  770.  
  771. // Stock check - only check if we're managing stock and backorders are not allowed 
  772. if ( ! $product_data->is_in_stock() ) { 
  773. throw new Exception( sprintf( __( 'You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_name() ) ); 
  774.  
  775. if ( ! $product_data->has_enough_stock( $quantity ) ) { 
  776. /** translators: 1: product name 2: quantity in stock */ 
  777. throw new Exception( sprintf( __( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ), $product_data->get_name(), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ) ) ); 
  778.  
  779. // Stock check - this time accounting for whats already in-cart 
  780. if ( $product_data->managing_stock() ) { 
  781. $products_qty_in_cart = $this->get_cart_item_quantities(); 
  782.  
  783. if ( isset( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] ) && ! $product_data->has_enough_stock( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] + $quantity ) ) { 
  784. throw new Exception( sprintf( 
  785. '<a href="%s" class="button wc-forward">%s</a> %s',  
  786. wc_get_cart_url(),  
  787. __( 'View Cart', 'woocommerce' ),  
  788. sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) ) 
  789. ) ); 
  790.  
  791. // If cart_item_key is set, the item is already in the cart 
  792. if ( $cart_item_key ) { 
  793. $new_quantity = $quantity + $this->cart_contents[ $cart_item_key ]['quantity']; 
  794. $this->set_quantity( $cart_item_key, $new_quantity, false ); 
  795. } else { 
  796. $cart_item_key = $cart_id; 
  797.  
  798. // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item 
  799. $this->cart_contents[ $cart_item_key ] = apply_filters( 'woocommerce_add_cart_item', array_merge( $cart_item_data, array( 
  800. 'product_id' => $product_id,  
  801. 'variation_id' => $variation_id,  
  802. 'variation' => $variation,  
  803. 'quantity' => $quantity,  
  804. 'data' => $product_data,  
  805. ) ), $cart_item_key ); 
  806.  
  807. if ( did_action( 'wp' ) ) { 
  808. $this->set_cart_cookies( ! $this->is_empty() ); 
  809.  
  810. do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ); 
  811.  
  812. return $cart_item_key; 
  813.  
  814. } catch ( Exception $e ) { 
  815. if ( $e->getMessage() ) { 
  816. wc_add_notice( $e->getMessage(), 'error' ); 
  817. return false; 
  818.  
  819. /** 
  820. * Remove a cart item. 
  821. * @since 2.3.0 
  822. * @param string $cart_item_key 
  823. * @return bool 
  824. */ 
  825. public function remove_cart_item( $cart_item_key ) { 
  826. if ( isset( $this->cart_contents[ $cart_item_key ] ) ) { 
  827. $this->removed_cart_contents[ $cart_item_key ] = $this->cart_contents[ $cart_item_key ]; 
  828. unset( $this->removed_cart_contents[ $cart_item_key ]['data'] ); 
  829.  
  830. do_action( 'woocommerce_remove_cart_item', $cart_item_key, $this ); 
  831.  
  832. unset( $this->cart_contents[ $cart_item_key ] ); 
  833.  
  834. do_action( 'woocommerce_cart_item_removed', $cart_item_key, $this ); 
  835.  
  836. $this->calculate_totals(); 
  837.  
  838. return true; 
  839.  
  840. return false; 
  841.  
  842. /** 
  843. * Restore a cart item. 
  844. * @param string $cart_item_key 
  845. * @return bool 
  846. */ 
  847. public function restore_cart_item( $cart_item_key ) { 
  848. if ( isset( $this->removed_cart_contents[ $cart_item_key ] ) ) { 
  849. $this->cart_contents[ $cart_item_key ] = $this->removed_cart_contents[ $cart_item_key ]; 
  850. $this->cart_contents[ $cart_item_key ]['data'] = wc_get_product( $this->cart_contents[ $cart_item_key ]['variation_id'] ? $this->cart_contents[ $cart_item_key ]['variation_id'] : $this->cart_contents[ $cart_item_key ]['product_id'] ); 
  851.  
  852. do_action( 'woocommerce_restore_cart_item', $cart_item_key, $this ); 
  853.  
  854. unset( $this->removed_cart_contents[ $cart_item_key ] ); 
  855.  
  856. do_action( 'woocommerce_cart_item_restored', $cart_item_key, $this ); 
  857.  
  858. $this->calculate_totals(); 
  859.  
  860. return true; 
  861.  
  862. return false; 
  863.  
  864. /** 
  865. * Set the quantity for an item in the cart. 
  866. * @param string $cart_item_key contains the id of the cart item 
  867. * @param int $quantity contains the quantity of the item 
  868. * @param bool $refresh_totals whether or not to calculate totals after setting the new qty 
  869. * @return bool 
  870. */ 
  871. public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) { 
  872. if ( 0 == $quantity || $quantity < 0 ) { 
  873. do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key ); 
  874. unset( $this->cart_contents[ $cart_item_key ] ); 
  875. } else { 
  876. $old_quantity = $this->cart_contents[ $cart_item_key ]['quantity']; 
  877. $this->cart_contents[ $cart_item_key ]['quantity'] = $quantity; 
  878. do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity ); 
  879.  
  880. if ( $refresh_totals ) { 
  881. $this->calculate_totals(); 
  882.  
  883. return true; 
  884.  
  885. /** 
  886. * Cart Calculation Functions. 
  887. */ 
  888.  
  889. /** 
  890. * Reset cart totals to the defaults. Useful before running calculations. 
  891. * @param bool $unset_session If true, the session data will be forced unset. 
  892. * @access private 
  893. */ 
  894. private function reset( $unset_session = false ) { 
  895. foreach ( $this->cart_session_data as $key => $default ) { 
  896. $this->$key = $default; 
  897. if ( $unset_session ) { 
  898. unset( WC()->session->$key ); 
  899. do_action( 'woocommerce_cart_reset', $this, $unset_session ); 
  900.  
  901. /** 
  902. * Sort by subtotal. 
  903. * @param array $a 
  904. * @param array $b 
  905. * @return int 
  906. */ 
  907. private function sort_by_subtotal( $a, $b ) { 
  908. $first_item_subtotal = isset( $a['line_subtotal'] ) ? $a['line_subtotal'] : 0; 
  909. $second_item_subtotal = isset( $b['line_subtotal'] ) ? $b['line_subtotal'] : 0; 
  910. if ( $first_item_subtotal === $second_item_subtotal ) { 
  911. return 0; 
  912. return ( $first_item_subtotal < $second_item_subtotal ) ? 1 : -1; 
  913.  
  914. /** 
  915. * Calculate totals for the items in the cart. 
  916. */ 
  917. public function calculate_totals() { 
  918. $this->reset(); 
  919. $this->coupons = $this->get_coupons(); 
  920.  
  921. do_action( 'woocommerce_before_calculate_totals', $this ); 
  922.  
  923. if ( $this->is_empty() ) { 
  924. $this->set_session(); 
  925. return; 
  926.  
  927. $tax_rates = array(); 
  928. $shop_tax_rates = array(); 
  929. $cart = $this->get_cart(); 
  930.  
  931. /** 
  932. * Calculate subtotals for items. This is done first so that discount logic can use the values. 
  933. */ 
  934. foreach ( $cart as $cart_item_key => $values ) { 
  935. $product = $values['data']; 
  936. $line_price = $product->get_price() * $values['quantity']; 
  937. $line_subtotal = 0; 
  938. $line_subtotal_tax = 0; 
  939.  
  940. /** 
  941. * No tax to calculate. 
  942. */ 
  943. if ( ! $product->is_taxable() ) { 
  944.  
  945. // Subtotal is the undiscounted price 
  946. $this->subtotal += $line_price; 
  947. $this->subtotal_ex_tax += $line_price; 
  948.  
  949. /** 
  950. * Prices include tax. 
  951. * To prevent rounding issues we need to work with the inclusive price where possible. 
  952. * otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would. 
  953. * be 8.325 leading to totals being 1p off. 
  954. * Pre tax coupons come off the price the customer thinks they are paying - tax is calculated. 
  955. * afterwards. 
  956. * e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that. 
  957. */ 
  958. } elseif ( $this->prices_include_tax ) { 
  959.  
  960. // Get base tax rates 
  961. if ( empty( $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ] ) ) { 
  962. $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ] = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); 
  963.  
  964. // Get item tax rates 
  965. if ( empty( $tax_rates[ $product->get_tax_class() ] ) ) { 
  966. $tax_rates[ $product->get_tax_class() ] = WC_Tax::get_rates( $product->get_tax_class() ); 
  967.  
  968. $base_tax_rates = $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ]; 
  969. $item_tax_rates = $tax_rates[ $product->get_tax_class() ]; 
  970.  
  971. /** 
  972. * ADJUST TAX - Calculations when base tax is not equal to the item tax. 
  973. * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations. 
  974. * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes. 
  975. * This feature is experimental @since 2.4.7 and may change in the future. Use at your risk. 
  976. */ 
  977. if ( $item_tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { 
  978.  
  979. // Work out a new base price without the shop's base tax 
  980. $taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true, true ); 
  981.  
  982. // Now we have a new item price (excluding TAX) 
  983. $line_subtotal = $line_price - array_sum( $taxes ); 
  984.  
  985. // Now add modified taxes 
  986. $tax_result = WC_Tax::calc_tax( $line_subtotal, $item_tax_rates ); 
  987. $line_subtotal_tax = array_sum( $tax_result ); 
  988.  
  989. /** 
  990. * Regular tax calculation (customer inside base and the tax class is unmodified. 
  991. */ 
  992. } else { 
  993.  
  994. // Calc tax normally 
  995. $taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates, true ); 
  996. $line_subtotal_tax = array_sum( $taxes ); 
  997. $line_subtotal = $line_price - array_sum( $taxes ); 
  998.  
  999. /** 
  1000. * Prices exclude tax. 
  1001. * This calculation is simpler - work with the base, untaxed price. 
  1002. */ 
  1003. } else { 
  1004.  
  1005. // Get item tax rates 
  1006. if ( empty( $tax_rates[ $product->get_tax_class() ] ) ) { 
  1007. $tax_rates[ $product->get_tax_class() ] = WC_Tax::get_rates( $product->get_tax_class() ); 
  1008.  
  1009. $item_tax_rates = $tax_rates[ $product->get_tax_class() ]; 
  1010.  
  1011. // Base tax for line before discount - we will store this in the order data 
  1012. $taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates ); 
  1013. $line_subtotal_tax = array_sum( $taxes ); 
  1014.  
  1015. $line_subtotal = $line_price; 
  1016.  
  1017. // Add to main subtotal 
  1018. $this->subtotal += $line_subtotal + $line_subtotal_tax; 
  1019. $this->subtotal_ex_tax += $line_subtotal; 
  1020.  
  1021. // Order cart items by price so coupon logic is 'fair' for customers and not based on order added to cart. 
  1022. uasort( $cart, apply_filters( 'woocommerce_sort_by_subtotal_callback', array( $this, 'sort_by_subtotal' ) ) ); 
  1023.  
  1024. /** 
  1025. * Calculate totals for items. 
  1026. */ 
  1027. foreach ( $cart as $cart_item_key => $values ) { 
  1028.  
  1029. $product = $values['data']; 
  1030.  
  1031. // Prices 
  1032. $base_price = $product->get_price(); 
  1033. $line_price = $product->get_price() * $values['quantity']; 
  1034.  
  1035. // Tax data 
  1036. $taxes = array(); 
  1037. $discounted_taxes = array(); 
  1038.  
  1039. /** 
  1040. * No tax to calculate. 
  1041. */ 
  1042. if ( ! $product->is_taxable() ) { 
  1043.  
  1044. // Discounted Price (price with any pre-tax discounts applied) 
  1045. $discounted_price = $this->get_discounted_price( $values, $base_price, true ); 
  1046. $line_subtotal_tax = 0; 
  1047. $line_subtotal = $line_price; 
  1048. $line_tax = 0; 
  1049. $line_total = round( $discounted_price * $values['quantity'], wc_get_rounding_precision() ); 
  1050.  
  1051. /** 
  1052. * Prices include tax. 
  1053. */ 
  1054. } elseif ( $this->prices_include_tax ) { 
  1055.  
  1056. $base_tax_rates = $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ]; 
  1057. $item_tax_rates = $tax_rates[ $product->get_tax_class() ]; 
  1058.  
  1059. /** 
  1060. * ADJUST TAX - Calculations when base tax is not equal to the item tax. 
  1061. * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations. 
  1062. * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes. 
  1063. * This feature is experimental @since 2.4.7 and may change in the future. Use at your risk. 
  1064. */ 
  1065. if ( $item_tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { 
  1066.  
  1067. // Work out a new base price without the shop's base tax 
  1068. $taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true, true ); 
  1069.  
  1070. // Now we have a new item price (excluding TAX) 
  1071. $line_subtotal = round( $line_price - array_sum( $taxes ), wc_get_rounding_precision() ); 
  1072. $taxes = WC_Tax::calc_tax( $line_subtotal, $item_tax_rates ); 
  1073. $line_subtotal_tax = array_sum( $taxes ); 
  1074.  
  1075. // Adjusted price (this is the price including the new tax rate) 
  1076. $adjusted_price = ( $line_subtotal + $line_subtotal_tax ) / $values['quantity']; 
  1077.  
  1078. // Apply discounts and get the discounted price FOR A SINGLE ITEM 
  1079. $discounted_price = $this->get_discounted_price( $values, $adjusted_price, true ); 
  1080.  
  1081. // Convert back to line price 
  1082. $discounted_line_price = $discounted_price * $values['quantity']; 
  1083.  
  1084. // Now use rounded line price to get taxes. 
  1085. $discounted_taxes = WC_Tax::calc_tax( $discounted_line_price, $item_tax_rates, true ); 
  1086. $line_tax = array_sum( $discounted_taxes ); 
  1087. $line_total = $discounted_line_price - $line_tax; 
  1088.  
  1089. /** 
  1090. * Regular tax calculation (customer inside base and the tax class is unmodified. 
  1091. */ 
  1092. } else { 
  1093.  
  1094. // Work out a new base price without the item tax 
  1095. $taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates, true ); 
  1096.  
  1097. // Now we have a new item price (excluding TAX) 
  1098. $line_subtotal = $line_price - array_sum( $taxes ); 
  1099. $line_subtotal_tax = array_sum( $taxes ); 
  1100.  
  1101. // Calc prices and tax (discounted) 
  1102. $discounted_price = $this->get_discounted_price( $values, $base_price, true ); 
  1103.  
  1104. // Convert back to line price 
  1105. $discounted_line_price = $discounted_price * $values['quantity']; 
  1106.  
  1107. // Now use rounded line price to get taxes. 
  1108. $discounted_taxes = WC_Tax::calc_tax( $discounted_line_price, $item_tax_rates, true ); 
  1109. $line_tax = array_sum( $discounted_taxes ); 
  1110. $line_total = $discounted_line_price - $line_tax; 
  1111.  
  1112. // Tax rows - merge the totals we just got 
  1113. foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) { 
  1114. $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); 
  1115.  
  1116. /** 
  1117. * Prices exclude tax. 
  1118. */ 
  1119. } else { 
  1120.  
  1121. $item_tax_rates = $tax_rates[ $product->get_tax_class() ]; 
  1122.  
  1123. // Work out a new base price without the shop's base tax 
  1124. $taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates ); 
  1125.  
  1126. // Now we have the item price (excluding TAX) 
  1127. $line_subtotal = $line_price; 
  1128. $line_subtotal_tax = array_sum( $taxes ); 
  1129.  
  1130. // Now calc product rates 
  1131. $discounted_price = $this->get_discounted_price( $values, $base_price, true ); 
  1132. $discounted_taxes = WC_Tax::calc_tax( $discounted_price * $values['quantity'], $item_tax_rates ); 
  1133. $discounted_tax_amount = array_sum( $discounted_taxes ); 
  1134. $line_tax = $discounted_tax_amount; 
  1135. $line_total = $discounted_price * $values['quantity']; 
  1136.  
  1137. // Tax rows - merge the totals we just got 
  1138. foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) { 
  1139. $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); 
  1140.  
  1141. // Cart contents total is based on discounted prices and is used for the final total calculation 
  1142. $this->cart_contents_total += $line_total; 
  1143.  
  1144. /** 
  1145. * Store costs + taxes for lines. For tax inclusive prices, we do some extra rounding logic so the stored 
  1146. * values "add up" when viewing the order in admin. This does have the disadvatage of not being able to 
  1147. * recalculate the tax total/subtotal accurately in the future, but it does ensure the data looks correct. 
  1148. * Tax exclusive prices are not affected. 
  1149. */ 
  1150. if ( ! $product->is_taxable() || $this->prices_include_tax ) { 
  1151. $this->cart_contents[ $cart_item_key ]['line_total'] = round( $line_total + $line_tax - wc_round_tax_total( $line_tax ), $this->dp ); 
  1152. $this->cart_contents[ $cart_item_key ]['line_subtotal'] = round( $line_subtotal + $line_subtotal_tax - wc_round_tax_total( $line_subtotal_tax ), $this->dp ); 
  1153. $this->cart_contents[ $cart_item_key ]['line_tax'] = wc_round_tax_total( $line_tax ); 
  1154. $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = wc_round_tax_total( $line_subtotal_tax ); 
  1155. $this->cart_contents[ $cart_item_key ]['line_tax_data'] = array( 'total' => array_map( 'wc_round_tax_total', $discounted_taxes ), 'subtotal' => array_map( 'wc_round_tax_total', $taxes ) ); 
  1156. } else { 
  1157. $this->cart_contents[ $cart_item_key ]['line_total'] = $line_total; 
  1158. $this->cart_contents[ $cart_item_key ]['line_subtotal'] = $line_subtotal; 
  1159. $this->cart_contents[ $cart_item_key ]['line_tax'] = $line_tax; 
  1160. $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $line_subtotal_tax; 
  1161. $this->cart_contents[ $cart_item_key ]['line_tax_data'] = array( 'total' => $discounted_taxes, 'subtotal' => $taxes ); 
  1162.  
  1163. // Only calculate the grand total + shipping if on the cart/checkout 
  1164. if ( is_checkout() || is_cart() || defined( 'WOOCOMMERCE_CHECKOUT' ) || defined( 'WOOCOMMERCE_CART' ) ) { 
  1165.  
  1166. // Calculate the Shipping 
  1167. $this->calculate_shipping(); 
  1168.  
  1169. // Trigger the fees API where developers can add fees to the cart 
  1170. $this->calculate_fees(); 
  1171.  
  1172. // Total up/round taxes and shipping taxes 
  1173. if ( $this->round_at_subtotal ) { 
  1174. $this->tax_total = WC_Tax::get_tax_total( $this->taxes ); 
  1175. $this->shipping_tax_total = WC_Tax::get_tax_total( $this->shipping_taxes ); 
  1176. $this->taxes = array_map( array( 'WC_Tax', 'round' ), $this->taxes ); 
  1177. $this->shipping_taxes = array_map( array( 'WC_Tax', 'round' ), $this->shipping_taxes ); 
  1178. } else { 
  1179. $this->tax_total = array_sum( $this->taxes ); 
  1180. $this->shipping_tax_total = array_sum( $this->shipping_taxes ); 
  1181.  
  1182. // VAT exemption done at this point - so all totals are correct before exemption 
  1183. if ( WC()->customer->get_is_vat_exempt() ) { 
  1184. $this->remove_taxes(); 
  1185.  
  1186. // Allow plugins to hook and alter totals before final total is calculated 
  1187. do_action( 'woocommerce_calculate_totals', $this ); 
  1188.  
  1189. // Grand Total - Discounted product prices, discounted tax, shipping cost + tax 
  1190. $this->total = max( 0, apply_filters( 'woocommerce_calculated_total', round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ), $this ) ); 
  1191.  
  1192. } else { 
  1193.  
  1194. // Set tax total to sum of all tax rows 
  1195. $this->tax_total = WC_Tax::get_tax_total( $this->taxes ); 
  1196.  
  1197. // VAT exemption done at this point - so all totals are correct before exemption 
  1198. if ( WC()->customer->get_is_vat_exempt() ) { 
  1199. $this->remove_taxes(); 
  1200.  
  1201. do_action( 'woocommerce_after_calculate_totals', $this ); 
  1202.  
  1203. $this->set_session(); 
  1204.  
  1205. /** 
  1206. * Remove taxes. 
  1207. */ 
  1208. public function remove_taxes() { 
  1209. $this->shipping_tax_total = $this->tax_total = 0; 
  1210. $this->subtotal = $this->subtotal_ex_tax; 
  1211.  
  1212. foreach ( $this->cart_contents as $cart_item_key => $item ) { 
  1213. $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $this->cart_contents[ $cart_item_key ]['line_tax'] = 0; 
  1214. $this->cart_contents[ $cart_item_key ]['line_tax_data'] = array( 'total' => array(), 'subtotal' => array() ); 
  1215.  
  1216. // If true, zero rate is applied so '0' tax is displayed on the frontend rather than nothing. 
  1217. if ( apply_filters( 'woocommerce_cart_remove_taxes_apply_zero_rate', true ) ) { 
  1218. $this->taxes = $this->shipping_taxes = array( apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) => 0 ); 
  1219. } else { 
  1220. $this->taxes = $this->shipping_taxes = array(); 
  1221.  
  1222. /** 
  1223. * Looks at the totals to see if payment is actually required. 
  1224. * @return bool 
  1225. */ 
  1226. public function needs_payment() { 
  1227. return apply_filters( 'woocommerce_cart_needs_payment', $this->total > 0, $this ); 
  1228.  
  1229. /** 
  1230. * Shipping related functions. 
  1231. */ 
  1232.  
  1233. /** 
  1234. * Uses the shipping class to calculate shipping then gets the totals when its finished. 
  1235. */ 
  1236. public function calculate_shipping() { 
  1237. if ( $this->needs_shipping() && $this->show_shipping() ) { 
  1238. WC()->shipping->calculate_shipping( $this->get_shipping_packages() ); 
  1239. } else { 
  1240. WC()->shipping->reset_shipping(); 
  1241.  
  1242. // Get totals for the chosen shipping method 
  1243. $this->shipping_total = WC()->shipping->shipping_total; // Shipping Total 
  1244. $this->shipping_taxes = WC()->shipping->shipping_taxes; // Shipping Taxes 
  1245.  
  1246. /** 
  1247. * Filter items needing shipping callback. 
  1248. * @since 3.0.0 
  1249. * @param array $item 
  1250. * @return bool 
  1251. */ 
  1252. protected function filter_items_needing_shipping( $item ) { 
  1253. $product = $item['data']; 
  1254. return $product && $product->needs_shipping(); 
  1255.  
  1256. /** 
  1257. * Get only items that need shipping. 
  1258. * @since 3.0.0 
  1259. * @return array 
  1260. */ 
  1261. protected function get_items_needing_shipping() { 
  1262. return array_filter( $this->get_cart(), array( $this, 'filter_items_needing_shipping' ) ); 
  1263.  
  1264. /** 
  1265. * Get packages to calculate shipping for. 
  1266. * This lets us calculate costs for carts that are shipped to multiple locations. 
  1267. * Shipping methods are responsible for looping through these packages. 
  1268. * By default we pass the cart itself as a package - plugins can change this. 
  1269. * through the filter and break it up. 
  1270. * @since 1.5.4 
  1271. * @return array of cart items 
  1272. */ 
  1273. public function get_shipping_packages() { 
  1274. return apply_filters( 'woocommerce_cart_shipping_packages',  
  1275. array( 
  1276. array( 
  1277. 'contents' => $this->get_items_needing_shipping(),  
  1278. 'contents_cost' => array_sum( wp_list_pluck( $this->get_items_needing_shipping(), 'line_total' ) ),  
  1279. 'applied_coupons' => $this->applied_coupons,  
  1280. 'user' => array( 
  1281. 'ID' => get_current_user_id(),  
  1282. ),  
  1283. 'destination' => array( 
  1284. 'country' => WC()->customer->get_shipping_country(),  
  1285. 'state' => WC()->customer->get_shipping_state(),  
  1286. 'postcode' => WC()->customer->get_shipping_postcode(),  
  1287. 'city' => WC()->customer->get_shipping_city(),  
  1288. 'address' => WC()->customer->get_shipping_address(),  
  1289. 'address_2' => WC()->customer->get_shipping_address_2(),  
  1290. ),  
  1291. ),  
  1292. ); 
  1293.  
  1294. /** 
  1295. * Looks through the cart to see if shipping is actually required. 
  1296. * @return bool whether or not the cart needs shipping 
  1297. */ 
  1298. public function needs_shipping() { 
  1299. if ( ! wc_shipping_enabled() || 0 === wc_get_shipping_method_count( true ) ) { 
  1300. return false; 
  1301. $needs_shipping = false; 
  1302.  
  1303. if ( ! empty( $this->cart_contents ) ) { 
  1304. foreach ( $this->cart_contents as $cart_item_key => $values ) { 
  1305. if ( $values['data']->needs_shipping() ) { 
  1306. $needs_shipping = true; 
  1307. break; 
  1308.  
  1309. return apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping ); 
  1310.  
  1311. /** 
  1312. * Should the shipping address form be shown. 
  1313. * @return bool 
  1314. */ 
  1315. public function needs_shipping_address() { 
  1316.  
  1317. $needs_shipping_address = false; 
  1318.  
  1319. if ( $this->needs_shipping() === true && ! wc_ship_to_billing_address_only() ) { 
  1320. $needs_shipping_address = true; 
  1321.  
  1322. return apply_filters( 'woocommerce_cart_needs_shipping_address', $needs_shipping_address ); 
  1323.  
  1324. /** 
  1325. * Sees if the customer has entered enough data to calc the shipping yet. 
  1326. * @return bool 
  1327. */ 
  1328. public function show_shipping() { 
  1329. if ( ! wc_shipping_enabled() || ! is_array( $this->cart_contents ) ) 
  1330. return false; 
  1331.  
  1332. if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) { 
  1333. if ( ! WC()->customer->has_calculated_shipping() ) { 
  1334. if ( ! WC()->customer->get_shipping_country() || ( ! WC()->customer->get_shipping_state() && ! WC()->customer->get_shipping_postcode() ) ) { 
  1335. return false; 
  1336.  
  1337. return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true ); 
  1338.  
  1339. /** 
  1340. * Sees if we need a shipping address. 
  1341. * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only() 
  1342. * @return bool 
  1343. */ 
  1344. public function ship_to_billing_address_only() { 
  1345. return wc_ship_to_billing_address_only(); 
  1346.  
  1347. /** 
  1348. * Gets the shipping total (after calculation). 
  1349. * @return string price or string for the shipping total 
  1350. */ 
  1351. public function get_cart_shipping_total() { 
  1352. if ( isset( $this->shipping_total ) ) { 
  1353. if ( $this->shipping_total > 0 ) { 
  1354.  
  1355. // Display varies depending on settings 
  1356. if ( 'excl' === $this->tax_display_cart ) { 
  1357.  
  1358. $return = wc_price( $this->shipping_total ); 
  1359.  
  1360. if ( $this->shipping_tax_total > 0 && $this->prices_include_tax ) { 
  1361. $return .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; 
  1362.  
  1363. return $return; 
  1364.  
  1365. } else { 
  1366.  
  1367. $return = wc_price( $this->shipping_total + $this->shipping_tax_total ); 
  1368.  
  1369. if ( $this->shipping_tax_total > 0 && ! $this->prices_include_tax ) { 
  1370. $return .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; 
  1371.  
  1372. return $return; 
  1373.  
  1374. } else { 
  1375. return __( 'Free!', 'woocommerce' ); 
  1376.  
  1377. return ''; 
  1378.  
  1379. /** 
  1380. * Coupons/Discount related functions. 
  1381. */ 
  1382.  
  1383. /** 
  1384. * Check for user coupons (now that we have billing email). If a coupon is invalid, add an error. 
  1385. * Checks two types of coupons: 
  1386. * 1. Where a list of customer emails are set (limits coupon usage to those defined). 
  1387. * 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email). 
  1388. * @param array $posted 
  1389. */ 
  1390. public function check_customer_coupons( $posted ) { 
  1391. if ( ! empty( $this->applied_coupons ) ) { 
  1392. foreach ( $this->applied_coupons as $code ) { 
  1393. $coupon = new WC_Coupon( $code ); 
  1394.  
  1395. if ( $coupon->is_valid() ) { 
  1396.  
  1397. // Limit to defined email addresses 
  1398. if ( is_array( $coupon->get_email_restrictions() ) && sizeof( $coupon->get_email_restrictions() ) > 0 ) { 
  1399. $check_emails = array(); 
  1400. if ( is_user_logged_in() ) { 
  1401. $current_user = wp_get_current_user(); 
  1402. $check_emails[] = $current_user->user_email; 
  1403. $check_emails[] = $posted['billing_email']; 
  1404. $check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) ); 
  1405.  
  1406. if ( 0 == sizeof( array_intersect( $check_emails, $coupon->get_email_restrictions() ) ) ) { 
  1407. $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ); 
  1408.  
  1409. // Remove the coupon 
  1410. $this->remove_coupon( $code ); 
  1411.  
  1412. // Flag totals for refresh 
  1413. WC()->session->set( 'refresh_totals', true ); 
  1414.  
  1415. // Usage limits per user - check against billing and user email and user ID 
  1416. if ( $coupon->get_usage_limit_per_user() > 0 ) { 
  1417. $check_emails = array(); 
  1418. $used_by = $coupon->get_used_by(); 
  1419.  
  1420. if ( is_user_logged_in() ) { 
  1421. $current_user = wp_get_current_user(); 
  1422. $check_emails[] = sanitize_email( $current_user->user_email ); 
  1423. $usage_count = sizeof( array_keys( $used_by, get_current_user_id() ) ); 
  1424. } else { 
  1425. $check_emails[] = sanitize_email( $posted['billing_email'] ); 
  1426. $user = get_user_by( 'email', $posted['billing_email'] ); 
  1427. if ( $user ) { 
  1428. $usage_count = sizeof( array_keys( $used_by, $user->ID ) ); 
  1429. } else { 
  1430. $usage_count = 0; 
  1431.  
  1432. foreach ( $check_emails as $check_email ) { 
  1433. $usage_count = $usage_count + sizeof( array_keys( $used_by, $check_email ) ); 
  1434.  
  1435. if ( $usage_count >= $coupon->get_usage_limit_per_user() ) { 
  1436. $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ); 
  1437.  
  1438. // Remove the coupon 
  1439. $this->remove_coupon( $code ); 
  1440.  
  1441. // Flag totals for refresh 
  1442. WC()->session->set( 'refresh_totals', true ); 
  1443.  
  1444. /** 
  1445. * Returns whether or not a discount has been applied. 
  1446. * @param string $coupon_code 
  1447. * @return bool 
  1448. */ 
  1449. public function has_discount( $coupon_code = '' ) { 
  1450. return $coupon_code ? in_array( wc_format_coupon_code( $coupon_code ), $this->applied_coupons ) : sizeof( $this->applied_coupons ) > 0; 
  1451.  
  1452. /** 
  1453. * Applies a coupon code passed to the method. 
  1454. * @param string $coupon_code - The code to apply 
  1455. * @return bool True if the coupon is applied, false if it does not exist or cannot be applied 
  1456. */ 
  1457. public function add_discount( $coupon_code ) { 
  1458. // Coupons are globally disabled. 
  1459. if ( ! wc_coupons_enabled() ) { 
  1460. return false; 
  1461.  
  1462. // Sanitize coupon code. 
  1463. $coupon_code = wc_format_coupon_code( $coupon_code ); 
  1464.  
  1465. // Get the coupon. 
  1466. $the_coupon = new WC_Coupon( $coupon_code ); 
  1467.  
  1468. // Check it can be used with cart. 
  1469. if ( ! $the_coupon->is_valid() ) { 
  1470. wc_add_notice( $the_coupon->get_error_message(), 'error' ); 
  1471. return false; 
  1472.  
  1473. // Check if applied. 
  1474. if ( $this->has_discount( $coupon_code ) ) { 
  1475. $the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED ); 
  1476. return false; 
  1477.  
  1478. // If its individual use then remove other coupons. 
  1479. if ( $the_coupon->get_individual_use() ) { 
  1480. $coupons_to_keep = apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons ); 
  1481.  
  1482. foreach ( $this->applied_coupons as $applied_coupon ) { 
  1483. $keep_key = array_search( $applied_coupon, $coupons_to_keep ); 
  1484. if ( false === $keep_key ) { 
  1485. $this->remove_coupon( $applied_coupon ); 
  1486. } else { 
  1487. unset( $coupons_to_keep[ $keep_key ] ); 
  1488.  
  1489. if ( ! empty( $coupons_to_keep ) ) { 
  1490. $this->applied_coupons += $coupons_to_keep; 
  1491.  
  1492. // Check to see if an individual use coupon is set. 
  1493. if ( $this->applied_coupons ) { 
  1494. foreach ( $this->applied_coupons as $code ) { 
  1495. $coupon = new WC_Coupon( $code ); 
  1496.  
  1497. if ( $coupon->get_individual_use() && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $coupon, $this->applied_coupons ) ) { 
  1498.  
  1499. // Reject new coupon. 
  1500. $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY ); 
  1501.  
  1502. return false; 
  1503.  
  1504. $this->applied_coupons[] = $coupon_code; 
  1505.  
  1506. // Choose free shipping. 
  1507. if ( $the_coupon->get_free_shipping() ) { 
  1508. $packages = WC()->shipping->get_packages(); 
  1509. $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); 
  1510.  
  1511. foreach ( $packages as $i => $package ) { 
  1512. $chosen_shipping_methods[ $i ] = 'free_shipping'; 
  1513.  
  1514. WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); 
  1515.  
  1516. $the_coupon->add_coupon_message( WC_Coupon::WC_COUPON_SUCCESS ); 
  1517.  
  1518. do_action( 'woocommerce_applied_coupon', $coupon_code ); 
  1519.  
  1520. return true; 
  1521.  
  1522. /** 
  1523. * Get array of applied coupon objects and codes. 
  1524. * @return array of applied coupons 
  1525. */ 
  1526. public function get_coupons( $deprecated = null ) { 
  1527. $coupons = array(); 
  1528.  
  1529. if ( 'order' === $deprecated ) { 
  1530. return $coupons; 
  1531.  
  1532. foreach ( $this->get_applied_coupons() as $code ) { 
  1533. $coupon = new WC_Coupon( $code ); 
  1534. $coupons[ $code ] = $coupon; 
  1535.  
  1536. return $coupons; 
  1537.  
  1538. /** 
  1539. * Gets the array of applied coupon codes. 
  1540. * @return array of applied coupons 
  1541. */ 
  1542. public function get_applied_coupons() { 
  1543. return $this->applied_coupons; 
  1544.  
  1545. /** 
  1546. * Get the discount amount for a used coupon. 
  1547. * @param string $code coupon code 
  1548. * @param bool $ex_tax inc or ex tax 
  1549. * @return float discount amount 
  1550. */ 
  1551. public function get_coupon_discount_amount( $code, $ex_tax = true ) { 
  1552. $discount_amount = isset( $this->coupon_discount_amounts[ $code ] ) ? $this->coupon_discount_amounts[ $code ] : 0; 
  1553.  
  1554. if ( ! $ex_tax ) { 
  1555. $discount_amount += $this->get_coupon_discount_tax_amount( $code ); 
  1556.  
  1557. return wc_cart_round_discount( $discount_amount, $this->dp ); 
  1558.  
  1559. /** 
  1560. * Get the discount tax amount for a used coupon (for tax inclusive prices). 
  1561. * @param string $code coupon code 
  1562. * @param bool inc or ex tax 
  1563. * @return float discount amount 
  1564. */ 
  1565. public function get_coupon_discount_tax_amount( $code ) { 
  1566. return wc_cart_round_discount( isset( $this->coupon_discount_tax_amounts[ $code ] ) ? $this->coupon_discount_tax_amounts[ $code ] : 0, $this->dp ); 
  1567.  
  1568. /** 
  1569. * Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax. 
  1570. */ 
  1571. public function remove_coupons( $deprecated = null ) { 
  1572. $this->applied_coupons = $this->coupon_discount_amounts = $this->coupon_discount_tax_amounts = $this->coupon_applied_count = array(); 
  1573. WC()->session->set( 'applied_coupons', array() ); 
  1574. WC()->session->set( 'coupon_discount_amounts', array() ); 
  1575. WC()->session->set( 'coupon_discount_tax_amounts', array() ); 
  1576.  
  1577. /** 
  1578. * Remove a single coupon by code. 
  1579. * @param string $coupon_code Code of the coupon to remove 
  1580. * @return bool 
  1581. */ 
  1582. public function remove_coupon( $coupon_code ) { 
  1583. // Coupons are globally disabled 
  1584. if ( ! wc_coupons_enabled() ) { 
  1585. return false; 
  1586.  
  1587. // Get the coupon 
  1588. $coupon_code = wc_format_coupon_code( $coupon_code ); 
  1589. $position = array_search( $coupon_code, $this->applied_coupons ); 
  1590.  
  1591. if ( false !== $position ) { 
  1592. unset( $this->applied_coupons[ $position ] ); 
  1593.  
  1594. WC()->session->set( 'applied_coupons', $this->applied_coupons ); 
  1595.  
  1596. do_action( 'woocommerce_removed_coupon', $coupon_code ); 
  1597.  
  1598. return true; 
  1599.  
  1600. /** 
  1601. * Function to apply discounts to a product and get the discounted price (before tax is applied). 
  1602. * @param mixed $values 
  1603. * @param mixed $price 
  1604. * @param bool $add_totals (default: false) 
  1605. * @return float price 
  1606. */ 
  1607. public function get_discounted_price( $values, $price, $add_totals = false ) { 
  1608. if ( ! $price ) { 
  1609. return $price; 
  1610.  
  1611. $undiscounted_price = $price; 
  1612.  
  1613. if ( ! empty( $this->coupons ) ) { 
  1614. $product = $values['data']; 
  1615.  
  1616. foreach ( $this->coupons as $code => $coupon ) { 
  1617. if ( $coupon->is_valid() && ( $coupon->is_valid_for_product( $product, $values ) || $coupon->is_valid_for_cart() ) ) { 
  1618. $discount_amount = $coupon->get_discount_amount( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ? $price : $undiscounted_price, $values, true ); 
  1619. $discount_amount = min( $price, $discount_amount ); 
  1620. $price = max( $price - $discount_amount, 0 ); 
  1621.  
  1622. // Store the totals for DISPLAY in the cart. 
  1623. if ( $add_totals ) { 
  1624. $total_discount = $discount_amount * $values['quantity']; 
  1625. $total_discount_tax = 0; 
  1626.  
  1627. if ( wc_tax_enabled() && $product->is_taxable() ) { 
  1628. $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); 
  1629. $taxes = WC_Tax::calc_tax( $discount_amount, $tax_rates, $this->prices_include_tax ); 
  1630. $total_discount_tax = WC_Tax::get_tax_total( $taxes ) * $values['quantity']; 
  1631. $total_discount = $this->prices_include_tax ? $total_discount - $total_discount_tax : $total_discount; 
  1632. $this->discount_cart_tax += $total_discount_tax; 
  1633.  
  1634. $this->discount_cart += $total_discount; 
  1635. $this->increase_coupon_discount_amount( $code, $total_discount, $total_discount_tax ); 
  1636. $this->increase_coupon_applied_count( $code, $values['quantity'] ); 
  1637.  
  1638. // If the price is 0, we can stop going through coupons because there is nothing more to discount for this product. 
  1639. if ( 0 >= $price ) { 
  1640. break; 
  1641.  
  1642. return apply_filters( 'woocommerce_get_discounted_price', $price, $values, $this ); 
  1643.  
  1644. /** 
  1645. * Store how much discount each coupon grants. 
  1646. * @access private 
  1647. * @param string $code 
  1648. * @param double $amount 
  1649. * @param double $tax 
  1650. */ 
  1651. private function increase_coupon_discount_amount( $code, $amount, $tax ) { 
  1652. $this->coupon_discount_amounts[ $code ] = isset( $this->coupon_discount_amounts[ $code ] ) ? $this->coupon_discount_amounts[ $code ] + $amount : $amount; 
  1653. $this->coupon_discount_tax_amounts[ $code ] = isset( $this->coupon_discount_tax_amounts[ $code ] ) ? $this->coupon_discount_tax_amounts[ $code ] + $tax : $tax; 
  1654.  
  1655. /** 
  1656. * Store how many times each coupon is applied to cart/items. 
  1657. * @access private 
  1658. * @param string $code 
  1659. * @param int $count 
  1660. */ 
  1661. private function increase_coupon_applied_count( $code, $count = 1 ) { 
  1662. if ( empty( $this->coupon_applied_count[ $code ] ) ) { 
  1663. $this->coupon_applied_count[ $code ] = 0; 
  1664. $this->coupon_applied_count[ $code ] += $count; 
  1665.  
  1666. /** 
  1667. * Fees API to add additional costs to orders. 
  1668. */ 
  1669.  
  1670. /** 
  1671. * Add additional fee to the cart. 
  1672. * Fee is an amount of money charged for a particular piece of work 
  1673. * or for a particular right or service, and not supposed to be negative. 
  1674. * @param string $name Unique name for the fee. Multiple fees of the same name cannot be added. 
  1675. * @param float $amount Fee amount (do not enter negative amounts). 
  1676. * @param bool $taxable Is the fee taxable? (default: false). 
  1677. * @param string $tax_class The tax class for the fee if taxable. A blank string is standard tax class. (default: ''). 
  1678. */ 
  1679. public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) { 
  1680.  
  1681. $new_fee_id = sanitize_title( $name ); 
  1682.  
  1683. // Only add each fee once. 
  1684. foreach ( $this->fees as $fee ) { 
  1685. if ( $fee->id == $new_fee_id ) { 
  1686. return; 
  1687.  
  1688. $new_fee = new stdClass(); 
  1689. $new_fee->id = $new_fee_id; 
  1690. $new_fee->name = esc_attr( $name ); 
  1691. $new_fee->amount = (float) esc_attr( $amount ); 
  1692. $new_fee->tax_class = $tax_class; 
  1693. $new_fee->taxable = $taxable ? true : false; 
  1694. $new_fee->tax = 0; 
  1695. $new_fee->tax_data = array(); 
  1696. $this->fees[] = $new_fee; 
  1697.  
  1698. /** 
  1699. * Get fees. 
  1700. * @return array 
  1701. */ 
  1702. public function get_fees() { 
  1703. return array_filter( (array) $this->fees ); 
  1704.  
  1705. /** 
  1706. * Calculate fees. 
  1707. */ 
  1708. public function calculate_fees() { 
  1709. // Reset fees before calculation 
  1710. $this->fee_total = 0; 
  1711. $this->fees = array(); 
  1712.  
  1713. // Fire an action where developers can add their fees 
  1714. do_action( 'woocommerce_cart_calculate_fees', $this ); 
  1715.  
  1716. // If fees were added, total them and calculate tax 
  1717. if ( ! empty( $this->fees ) ) { 
  1718. foreach ( $this->fees as $fee_key => $fee ) { 
  1719. $this->fee_total += $fee->amount; 
  1720.  
  1721. if ( $fee->taxable ) { 
  1722. // Get tax rates 
  1723. $tax_rates = WC_Tax::get_rates( $fee->tax_class ); 
  1724. $fee_taxes = WC_Tax::calc_tax( $fee->amount, $tax_rates, false ); 
  1725.  
  1726. if ( ! empty( $fee_taxes ) ) { 
  1727. // Set the tax total for this fee 
  1728. $this->fees[ $fee_key ]->tax = array_sum( $fee_taxes ); 
  1729.  
  1730. // Set tax data - Since 2.2 
  1731. $this->fees[ $fee_key ]->tax_data = $fee_taxes; 
  1732.  
  1733. // Tax rows - merge the totals we just got 
  1734. foreach ( array_keys( $this->taxes + $fee_taxes ) as $key ) { 
  1735. $this->taxes[ $key ] = ( isset( $fee_taxes[ $key ] ) ? $fee_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); 
  1736.  
  1737. /** 
  1738. * Get Formatted Totals. 
  1739. */ 
  1740.  
  1741. /** 
  1742. * Gets the order total (after calculation). 
  1743. * @return string formatted price 
  1744. */ 
  1745. public function get_total() { 
  1746. return apply_filters( 'woocommerce_cart_total', wc_price( $this->total ) ); 
  1747.  
  1748. /** 
  1749. * Gets the total excluding taxes. 
  1750. * @return string formatted price 
  1751. */ 
  1752. public function get_total_ex_tax() { 
  1753. $total = $this->total - $this->tax_total - $this->shipping_tax_total; 
  1754. if ( $total < 0 ) { 
  1755. $total = 0; 
  1756. return apply_filters( 'woocommerce_cart_total_ex_tax', wc_price( $total ) ); 
  1757.  
  1758. /** 
  1759. * Gets the cart contents total (after calculation). 
  1760. * @todo deprecate? It's unused. 
  1761. * @return string formatted price 
  1762. */ 
  1763. public function get_cart_total() { 
  1764. if ( ! $this->prices_include_tax ) { 
  1765. $cart_contents_total = wc_price( $this->cart_contents_total ); 
  1766. } else { 
  1767. $cart_contents_total = wc_price( $this->cart_contents_total + $this->tax_total ); 
  1768.  
  1769. return apply_filters( 'woocommerce_cart_contents_total', $cart_contents_total ); 
  1770.  
  1771. /** 
  1772. * Gets the sub total (after calculation). 
  1773. * @param bool $compound whether to include compound taxes 
  1774. * @return string formatted price 
  1775. */ 
  1776. public function get_cart_subtotal( $compound = false ) { 
  1777.  
  1778. // If the cart has compound tax, we want to show the subtotal as 
  1779. // cart + shipping + non-compound taxes (after discount) 
  1780. if ( $compound ) { 
  1781.  
  1782. $cart_subtotal = wc_price( $this->cart_contents_total + $this->shipping_total + $this->get_taxes_total( false, false ) ); 
  1783.  
  1784. // Otherwise we show cart items totals only (before discount) 
  1785. } else { 
  1786.  
  1787. // Display varies depending on settings 
  1788. if ( 'excl' === $this->tax_display_cart ) { 
  1789.  
  1790. $cart_subtotal = wc_price( $this->subtotal_ex_tax ); 
  1791.  
  1792. if ( $this->tax_total > 0 && $this->prices_include_tax ) { 
  1793. $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; 
  1794. } else { 
  1795.  
  1796. $cart_subtotal = wc_price( $this->subtotal ); 
  1797.  
  1798. if ( $this->tax_total > 0 && ! $this->prices_include_tax ) { 
  1799. $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; 
  1800.  
  1801. return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this ); 
  1802.  
  1803. /** 
  1804. * Get the product row price per item. 
  1805. * @param WC_Product $product 
  1806. * @return string formatted price 
  1807. */ 
  1808. public function get_product_price( $product ) { 
  1809. if ( 'excl' === $this->tax_display_cart ) { 
  1810. $product_price = wc_get_price_excluding_tax( $product ); 
  1811. } else { 
  1812. $product_price = wc_get_price_including_tax( $product ); 
  1813. return apply_filters( 'woocommerce_cart_product_price', wc_price( $product_price ), $product ); 
  1814.  
  1815. /** 
  1816. * Get the product row subtotal. 
  1817. * Gets the tax etc to avoid rounding issues. 
  1818. * When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate. 
  1819. * @param WC_Product $product 
  1820. * @param int $quantity 
  1821. * @return string formatted price 
  1822. */ 
  1823. public function get_product_subtotal( $product, $quantity ) { 
  1824. $price = $product->get_price(); 
  1825. $taxable = $product->is_taxable(); 
  1826.  
  1827. // Taxable 
  1828. if ( $taxable ) { 
  1829.  
  1830. if ( 'excl' === $this->tax_display_cart ) { 
  1831.  
  1832. $row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) ); 
  1833. $product_subtotal = wc_price( $row_price ); 
  1834.  
  1835. if ( $this->prices_include_tax && $this->tax_total > 0 ) { 
  1836. $product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; 
  1837. } else { 
  1838.  
  1839. $row_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) ); 
  1840. $product_subtotal = wc_price( $row_price ); 
  1841.  
  1842. if ( ! $this->prices_include_tax && $this->tax_total > 0 ) { 
  1843. $product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; 
  1844.  
  1845. // Non-taxable 
  1846. } else { 
  1847.  
  1848. $row_price = $price * $quantity; 
  1849. $product_subtotal = wc_price( $row_price ); 
  1850.  
  1851.  
  1852. return apply_filters( 'woocommerce_cart_product_subtotal', $product_subtotal, $product, $quantity, $this ); 
  1853.  
  1854. /** 
  1855. * Gets the cart tax (after calculation). 
  1856. * @return string formatted price 
  1857. */ 
  1858. public function get_cart_tax() { 
  1859. $cart_total_tax = wc_round_tax_total( $this->tax_total + $this->shipping_tax_total ); 
  1860.  
  1861. return apply_filters( 'woocommerce_get_cart_tax', $cart_total_tax ? wc_price( $cart_total_tax ) : '' ); 
  1862.  
  1863. /** 
  1864. * Get a tax amount. 
  1865. * @param string $tax_rate_id 
  1866. * @return float amount 
  1867. */ 
  1868. public function get_tax_amount( $tax_rate_id ) { 
  1869. return isset( $this->taxes[ $tax_rate_id ] ) ? $this->taxes[ $tax_rate_id ] : 0; 
  1870.  
  1871. /** 
  1872. * Get a tax amount. 
  1873. * @param string $tax_rate_id 
  1874. * @return float amount 
  1875. */ 
  1876. public function get_shipping_tax_amount( $tax_rate_id ) { 
  1877. return isset( $this->shipping_taxes[ $tax_rate_id ] ) ? $this->shipping_taxes[ $tax_rate_id ] : 0; 
  1878.  
  1879. /** 
  1880. * Get tax row amounts with or without compound taxes includes. 
  1881. * @param bool $compound True if getting compound taxes 
  1882. * @param bool $display True if getting total to display 
  1883. * @return float price 
  1884. */ 
  1885. public function get_taxes_total( $compound = true, $display = true ) { 
  1886. $total = 0; 
  1887. foreach ( $this->taxes as $key => $tax ) { 
  1888. if ( ! $compound && WC_Tax::is_compound( $key ) ) continue; 
  1889. $total += $tax; 
  1890. foreach ( $this->shipping_taxes as $key => $tax ) { 
  1891. if ( ! $compound && WC_Tax::is_compound( $key ) ) continue; 
  1892. $total += $tax; 
  1893. if ( $display ) { 
  1894. $total = wc_round_tax_total( $total ); 
  1895. return apply_filters( 'woocommerce_cart_taxes_total', $total, $compound, $display, $this ); 
  1896.  
  1897. /** 
  1898. * Get the total of all cart discounts. 
  1899. * @return float 
  1900. */ 
  1901. public function get_cart_discount_total() { 
  1902. return wc_cart_round_discount( $this->discount_cart, $this->dp ); 
  1903.  
  1904. /** 
  1905. * Get the total of all cart tax discounts (used for discounts on tax inclusive prices). 
  1906. * @return float 
  1907. */ 
  1908. public function get_cart_discount_tax_total() { 
  1909. return wc_cart_round_discount( $this->discount_cart_tax, $this->dp ); 
  1910.  
  1911. /** 
  1912. * Gets the total discount amount - both kinds. 
  1913. * @return mixed formatted price or false if there are none 
  1914. */ 
  1915. public function get_total_discount() { 
  1916. if ( $this->get_cart_discount_total() ) { 
  1917. $total_discount = wc_price( $this->get_cart_discount_total() ); 
  1918. } else { 
  1919. $total_discount = false; 
  1920. return apply_filters( 'woocommerce_cart_total_discount', $total_discount, $this ); 
  1921.  
  1922. /** 
  1923. * Gets the total (product) discount amount - these are applied before tax. 
  1924. * @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required. 
  1925. * @return mixed formatted price or false if there are none 
  1926. */ 
  1927. public function get_discounts_before_tax() { 
  1928. wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' ); 
  1929. if ( $this->get_cart_discount_total() ) { 
  1930. $discounts_before_tax = wc_price( $this->get_cart_discount_total() ); 
  1931. } else { 
  1932. $discounts_before_tax = false; 
  1933. return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this ); 
  1934.  
  1935. /** 
  1936. * Get the total of all order discounts (after tax discounts). 
  1937. * @deprecated Order discounts (after tax) removed in 2.3 
  1938. * @return int 
  1939. */ 
  1940. public function get_order_discount_total() { 
  1941. wc_deprecated_function( 'get_order_discount_total', '2.3' ); 
  1942. return 0; 
  1943.  
  1944. /** 
  1945. * Function to apply cart discounts after tax. 
  1946. * @deprecated Coupons can not be applied after tax 
  1947. */ 
  1948. public function apply_cart_discounts_after_tax( $values, $price ) { 
  1949. wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' ); 
  1950.  
  1951. /** 
  1952. * Function to apply product discounts after tax. 
  1953. * @deprecated Coupons can not be applied after tax 
  1954. */ 
  1955. public function apply_product_discounts_after_tax( $values, $price ) { 
  1956. wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' ); 
  1957.  
  1958. /** 
  1959. * Gets the order discount amount - these are applied after tax. 
  1960. * @deprecated Coupons can not be applied after tax 
  1961. */ 
  1962. public function get_discounts_after_tax() { 
  1963. wc_deprecated_function( 'get_discounts_after_tax', '2.3' );