/includes/class-wc-cart.php

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