diff --git a/includes/class-wc-cart-item.php b/includes/class-wc-cart-item.php deleted file mode 100644 index 694a87cd652..00000000000 --- a/includes/class-wc-cart-item.php +++ /dev/null @@ -1,218 +0,0 @@ - 0, - 'quantity' => 0, - 'variation' => array(), - ); - - /** - * Product this item represents. - * - * @var WC_Product - */ - protected $product = null; - - /** - * Constructor. - * - * @param array $data - */ - public function __construct( $data = array() ) { - $this->set_data( $data ); - } - - /** - * Gets price of the product. - * @return float - */ - public function get_price() { - return $this->get_product() ? $this->get_product()->get_price() : 0; - } - - /** - * Gets weight of the product. - * @return float - */ - public function get_weight() { - return $this->get_product() ? $this->get_product()->get_weight() : 0; - } - - /** - * Gets tax class of the product. - * @return float - */ - public function get_tax_class() { - return $this->get_product() ? $this->get_product()->get_tax_class() : ''; - } - - /** - * Set product. - * @param int $value - */ - public function set_product( $value ) { - $this->product = $value; - $this->data['product_id'] = is_callable( array( $this->product, 'get_variation_id' ) ) ? $this->product->get_variation_id() : $this->product->get_id(); - } - - /** - * Get product object. - * - * @return WC_Product - */ - public function get_product() { - return ! is_null( $this->product ) ? $this->product : ( $this->product = wc_get_product( $this->get_product_id() ) ); - } - - /** - * Get all item data. - * @return array - */ - public function get_data() { - return $this->data; - } - - /** - * Product or variation ID this item represents. - * @return int - */ - public function get_product_id() { - return $this->data['product_id']; - } - - /** - * Get quantity in cart. - * @return int - */ - public function get_quantity() { - return $this->data['quantity']; - } - - /** - * Get variation data. - * @return array - */ - public function get_variation() { - return $this->data['variation']; - } - - /** - * Set product ID. - * @param int $value - */ - public function set_product_id( $value ) { - $this->data['product_id'] = absint( $value ); - $this->product = null; - } - - /** - * Set Quantity. - * @param int $value - */ - public function set_quantity( $value ) { - $this->data['quantity'] = wc_stock_amount( $value ); - } - - /** - * Set variation data. - * @param array $value - */ - public function set_variation( $value ) { - $this->data['variation'] = (array) $value; - } - - /** - * Set all data. - * @param array $value - */ - public function set_data( $values ) { - if ( is_a( $values, 'WC_Cart_Item' ) ) { - $values = $values->get_data(); - } - foreach ( $values as $key => $value ) { - if ( in_array( $key, array( 'quantity', 'product_id', 'variation', 'product' ) ) ) { - $this->{ "set_$key" }( $value ); - } else { - $this->data[ $key ] = $value; - } - } - } - - /** - * ArrayAccess/Backwards compatibility. - * - * @param string $offset - * @return mixed - */ - public function offsetGet( $offset ) { - switch ( $offset ) { - case 'data' : - return $this->get_product(); - case 'variation_id' : - return is_callable( array( $this, 'get_variation_id' ) ) ? $this->get_product()->get_variation_id() : 0; - } - return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : ''; - } - - /** - * ArrayAccess/Backwards compatibility. - * - * @param string $offset - * @param mixed $value - */ - public function offsetSet( $offset, $value ) { - switch ( $offset ) { - case 'data' : - $this->set_product( $value ); - break; - case 'variation_id' : - $this->set_product( wc_get_product( $value ) ); - break; - default : - $this->data[ $offset ] = $value; - break; - } - } - - /** - * ArrayAccess/Backwards compatibility. - * - * @param string $offset - * @return bool - */ - public function offsetExists( $offset ) { - if ( in_array( $offset, array( 'data' ) ) || isset( $this->data[ $offset ] ) ) { - return true; - } - return false; - } - - /** - * ArrayAccess/Backwards compatibility. - * - * @param string $offset - */ - public function offsetUnset( $offset ) { - unset( $this->data[ $offset ] ); - } -} diff --git a/includes/class-wc-cart-totals.php b/includes/class-wc-cart-totals.php index c1bbc26b600..2362e1555b5 100644 --- a/includes/class-wc-cart-totals.php +++ b/includes/class-wc-cart-totals.php @@ -235,7 +235,7 @@ final class WC_Cart_Totals { $fee->total_tax = array_sum( $fee->taxes ); if ( ! $this->round_at_subtotal() ) { - $fee->total_tax = wc_round_tax_total( $fee->total_tax, 0 ); + $fee->total_tax = wc_round_tax_total( $fee->total_tax, wc_get_rounding_precision() ); } } @@ -259,7 +259,7 @@ final class WC_Cart_Totals { $shipping_line->total_tax = wc_add_number_precision_deep( array_sum( $shipping_object->taxes ) ); if ( ! $this->round_at_subtotal() ) { - $shipping_line->total_tax = wc_round_tax_total( $shipping_line->total_tax, 0 ); + $shipping_line->total_tax = wc_round_tax_total( $shipping_line->total_tax, wc_get_rounding_precision() ); } $this->shipping[ $key ] = $shipping_line; @@ -282,12 +282,14 @@ final class WC_Cart_Totals { * If the customer is outside of the base location, this removes the base * taxes. This is off by default unless the filter is used. * + * Uses edit context so unfiltered tax class is returned. + * * @since 3.2.0 * @param object $item Item to adjust the prices of. * @return object */ protected function adjust_non_base_location_price( $item ) { - $base_tax_rates = WC_Tax::get_base_tax_rates( $item->product->tax_class ); + $base_tax_rates = WC_Tax::get_base_tax_rates( $item->product->get_tax_class( 'edit' ) ); if ( $item->tax_rates !== $base_tax_rates ) { // Work out a new base price without the shop's base tax. @@ -308,7 +310,13 @@ final class WC_Cart_Totals { * @return int */ protected function get_discounted_price_in_cents( $item_key ) { - return $this->items[ $item_key ]->subtotal - $this->discount_totals[ $item_key ]; + $item = $this->items[ $item_key ]; + $price = $item->subtotal - $this->discount_totals[ $item_key ]; + + if ( $item->price_includes_tax ) { + $price += $item->subtotal_tax; + } + return $price; } /** @@ -424,7 +432,7 @@ final class WC_Cart_Totals { $item->total_tax = array_sum( $item->taxes ); if ( ! $this->round_at_subtotal() ) { - $item->total_tax = wc_round_tax_total( $item->total_tax, 0 ); + $item->total_tax = wc_round_tax_total( $item->total_tax, wc_get_rounding_precision() ); } if ( $item->price_includes_tax ) { @@ -433,13 +441,13 @@ final class WC_Cart_Totals { $item->total = $item->total; } } + + $this->object->cart_contents[ $item_key ]['line_total'] = wc_remove_number_precision( $item->total ); + $this->object->cart_contents[ $item_key ]['line_tax'] = wc_remove_number_precision( $item->total_tax ); } $this->set_total( 'items_total', array_sum( array_values( wp_list_pluck( $this->items, 'total' ) ) ) ); $this->set_total( 'items_total_tax', array_sum( array_values( wp_list_pluck( $this->items, 'total_tax' ) ) ) ); - - $this->object->subtotal = $this->get_total( 'items_total' ) + $this->get_total( 'items_total_tax' ); - $this->object->subtotal_ex_tax = $this->get_total( 'items_total' ); } /** @@ -457,28 +465,32 @@ final class WC_Cart_Totals { * @since 3.2.0 */ protected function calculate_item_subtotals() { - foreach ( $this->items as $item ) { + foreach ( $this->items as $item_key => $item ) { if ( $item->price_includes_tax && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { - $item = $this->adjust_non_base_location_price( $item ); + $item = $this->adjust_non_base_location_price( $item ); } + if ( $this->calculate_tax && $item->product->is_taxable() ) { $subtotal_taxes = WC_Tax::calc_tax( $item->subtotal, $item->tax_rates, $item->price_includes_tax ); $item->subtotal_tax = array_sum( $subtotal_taxes ); if ( ! $this->round_at_subtotal() ) { - $item->subtotal_tax = wc_round_tax_total( $item->subtotal_tax, 0 ); + $item->subtotal_tax = wc_round_tax_total( $item->subtotal_tax, wc_get_rounding_precision() ); } if ( $item->price_includes_tax ) { $item->subtotal = $item->subtotal - $item->subtotal_tax; } } + + $this->object->cart_contents[ $item_key ]['line_subtotal'] = wc_remove_number_precision( $item->subtotal ); + $this->object->cart_contents[ $item_key ]['line_subtotal_tax'] = wc_remove_number_precision( $item->subtotal_tax ); } $this->set_total( 'items_subtotal', array_sum( array_values( wp_list_pluck( $this->items, 'subtotal' ) ) ) ); $this->set_total( 'items_subtotal_tax', array_sum( array_values( wp_list_pluck( $this->items, 'subtotal_tax' ) ) ) ); - $this->object->subtotal = $this->get_total( 'items_total' ) + $this->get_total( 'items_total_tax' ); - $this->object->subtotal_ex_tax = $this->get_total( 'items_total' ); + $this->object->subtotal = $this->get_total( 'items_subtotal' ) + $this->get_total( 'items_subtotal_tax' ); + $this->object->subtotal_ex_tax = $this->get_total( 'items_subtotal' ); } /** @@ -496,15 +508,11 @@ final class WC_Cart_Totals { $discounts->apply_discount( $coupon ); } - $this->discount_totals = $discounts->get_discounts_by_item( true ); - $this->totals['discounts_total'] = ! empty( $this->discount_totals ) ? array_sum( $this->discount_totals ) : 0; - $this->object->coupon_discount_amounts = $discounts->get_discounts_by_coupon(); + $coupon_discount_amounts = $discounts->get_discounts_by_coupon( true ); + $coupon_discount_tax_amounts = array(); // See how much tax was 'discounted' per item and per coupon. if ( $this->calculate_tax ) { - $coupon_discount_tax_amounts = array(); - $item_taxes = 0; - foreach ( $discounts->get_discounts( true ) as $coupon_code => $coupon_discounts ) { $coupon_discount_tax_amounts[ $coupon_code ] = 0; @@ -512,16 +520,21 @@ final class WC_Cart_Totals { $item = $this->items[ $item_key ]; if ( $item->product->is_taxable() ) { - $item_tax = array_sum( WC_Tax::calc_tax( $item_discount, $item->tax_rates, false ) ); - $item_taxes += $item_tax; + $item_tax = array_sum( WC_Tax::calc_tax( $item_discount, $item->tax_rates, $item->price_includes_tax ) ); $coupon_discount_tax_amounts[ $coupon_code ] += $item_tax; } } - } - $this->totals['discounts_tax_total'] = $item_taxes; - $this->object->coupon_discount_tax_amounts = $coupon_discount_tax_amounts; + $coupon_discount_amounts[ $coupon_code ] -= $coupon_discount_tax_amounts[ $coupon_code ]; + } } + + $this->discount_totals = $discounts->get_discounts_by_item( true ); + $this->object->coupon_discount_amounts = wc_remove_number_precision_deep( $coupon_discount_amounts ); + $this->object->coupon_discount_tax_amounts = wc_remove_number_precision_deep( $coupon_discount_tax_amounts ); + + $this->set_total( 'discounts_total', ! empty( $this->discount_totals ) ? array_sum( $this->discount_totals ) : 0 ); + $this->set_total( 'discounts_tax_total', array_sum( $coupon_discount_tax_amounts ) ); } /** @@ -583,10 +596,13 @@ final class WC_Cart_Totals { $this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + $this->get_total( 'shipping_total', true ) + $this->get_total( 'tax_total', true ) + $this->get_total( 'shipping_tax_total', true ) ) ); // Add totals to cart object. - $this->object->taxes = wp_list_pluck( $this->get_total( 'taxes' ), 'shipping_tax_total' ); - $this->object->shipping_taxes = wp_list_pluck( $this->get_total( 'taxes' ), 'tax_total' ); - $this->object->tax_total = $this->get_total( 'tax_total' ); - $this->object->total = $this->get_total( 'total' ); + $this->object->taxes = wp_list_pluck( $this->get_total( 'taxes' ), 'shipping_tax_total' ); + $this->object->shipping_taxes = wp_list_pluck( $this->get_total( 'taxes' ), 'tax_total' ); + $this->object->cart_contents_total = $this->get_total( 'items_total' ); + $this->object->tax_total = $this->get_total( 'tax_total' ); + $this->object->total = $this->get_total( 'total' ); + $this->object->discount_cart = $this->get_total( 'discounts_total' ) - $this->get_total( 'discounts_tax_total' ); + $this->object->discount_cart_tax = $this->get_total( 'discounts_tax_total' ); // Allow plugins to hook and alter totals before final total is calculated. if ( has_action( 'woocommerce_calculate_totals' ) ) { diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 26d7c9d7f47..aa21212e003 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -1,86 +1,158 @@ 0, 'total' => 0, @@ -111,8 +183,8 @@ class WC_Cart extends WC_Legacy_Cart { */ public function __construct() { add_action( 'wp_loaded', array( $this, 'init' ) ); // Get cart after WP and plugins are loaded. - add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); // Set cookies - add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing + add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); // Set cookies. + add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing. add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 ); add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 ); } @@ -120,7 +192,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Auto-load in-accessible properties on demand. * - * @param mixed $key + * @param mixed $key Key to get. * @return mixed */ public function __get( $key ) { @@ -150,10 +222,12 @@ class WC_Cart extends WC_Legacy_Cart { case 'tax' : wc_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax:: directly' ); $this->tax = new WC_Tax(); - return $this->tax; + return $this->tax; case 'discount_total': 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/' ); - return 0; + return 0; + case 'coupons' : + return $this->get_coupons(); } } @@ -185,12 +259,12 @@ class WC_Cart extends WC_Legacy_Cart { * Set cart hash cookie and items in cart. * * @access private - * @param bool $set (default: true) + * @param bool $set Should cookies be set (true) or unset. */ private function set_cart_cookies( $set = true ) { if ( $set ) { wc_setcookie( 'woocommerce_items_in_cart', 1 ); - wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart_for_session() ) ) ); + wc_setcookie( 'woocommerce_cart_hash', md5( wp_json_encode( $this->get_cart_for_session() ) ) ); } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS ); wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS ); @@ -198,15 +272,10 @@ class WC_Cart extends WC_Legacy_Cart { do_action( 'woocommerce_set_cart_cookies', $set ); } - /* - /* Cart Session Handling - */ - /** * Get the cart data from the PHP session and store it in class variables. */ public function get_cart_from_session() { - // Load cart session data from session foreach ( $this->cart_session_data as $key => $default ) { $this->$key = WC()->session->get( $key, $default ); } @@ -228,7 +297,7 @@ class WC_Cart extends WC_Legacy_Cart { } if ( is_array( $cart ) ) { - // Prime meta cache to reduce future queries + // Prime meta cache to reduce future queries. update_meta_cache( 'post', wp_list_pluck( $cart, 'product_id' ) ); update_object_term_cache( wp_list_pluck( $cart, 'product_id' ), 'product' ); @@ -238,32 +307,28 @@ class WC_Cart extends WC_Legacy_Cart { if ( ! empty( $product ) && $product->exists() && $values['quantity'] > 0 ) { if ( ! $product->is_purchasable() ) { - - // Flag to indicate the stored cart should be update - $update_cart_session = true; + $update_cart_session = true; // Flag to indicate the stored cart should be updated. /* translators: %s: product name */ 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' ); do_action( 'woocommerce_remove_cart_item_from_session', $key, $values ); } else { - // Put session data into array. Run through filter so other plugins can load their own session data + // Put session data into array. Run through filter so other plugins can load their own session data. $session_data = array_merge( $values, array( 'data' => $product ) ); $this->cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key ); - } } } } - // Trigger action do_action( 'woocommerce_cart_loaded_from_session', $this ); if ( $update_cart_session ) { WC()->session->cart = $this->get_cart_for_session(); } - // Queue re-calc if subtotal is not set + // Queue re-calc if subtotal is not set. if ( ( ! $this->subtotal && ! $this->is_empty() ) || $update_cart_session ) { $this->calculate_totals(); } @@ -273,7 +338,6 @@ class WC_Cart extends WC_Legacy_Cart { * Sets the php session data for the cart and coupons. */ public function set_session() { - // Set cart and coupon session data $cart_session = $this->get_cart_for_session(); WC()->session->set( 'cart', $cart_session ); @@ -296,7 +360,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Empties the cart and optionally the persistent cart too. * - * @param bool $clear_persistent_cart (default: true) + * @param bool $clear_persistent_cart Should the persistant cart be cleared too. Defaults to true. */ public function empty_cart( $clear_persistent_cart = true ) { $this->cart_contents = array(); @@ -312,10 +376,6 @@ class WC_Cart extends WC_Legacy_Cart { do_action( 'woocommerce_cart_emptied' ); } - /* - * Persistent cart handling - */ - /** * Save the persistent cart when the cart is updated. */ @@ -332,23 +392,9 @@ class WC_Cart extends WC_Legacy_Cart { delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() ); } - /* - * Cart Data Functions. - */ - - /** - * Coupons enabled function. Filterable. - * - * @deprecated 2.5.0 in favor to wc_coupons_enabled() - * - * @return bool - */ - public function coupons_enabled() { - return wc_coupons_enabled(); - } - /** * Get number of items in the cart. + * * @return int */ public function get_cart_contents_count() { @@ -357,6 +403,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get weight of items in the cart. + * * @since 2.5.0 * @return int */ @@ -376,18 +423,14 @@ class WC_Cart extends WC_Legacy_Cart { * @return bool */ public function is_empty() { - return 0 === sizeof( $this->get_cart() ); + return 0 === count( $this->get_cart() ); } /** * Check all cart items for errors. */ public function check_cart_items() { - - // Result $return = true; - - // Check cart item validity $result = $this->check_cart_item_validity(); if ( is_wp_error( $result ) ) { @@ -395,7 +438,6 @@ class WC_Cart extends WC_Legacy_Cart { $return = false; } - // Check item stock $result = $this->check_cart_item_stock(); if ( is_wp_error( $result ) ) { @@ -415,13 +457,10 @@ class WC_Cart extends WC_Legacy_Cart { $coupon = new WC_Coupon( $code ); if ( ! $coupon->is_valid() ) { - // Error message $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED ); - - // Remove the coupon $this->remove_coupon( $code ); - // Flag totals for refresh + // Flag totals for refresh. WC()->session->set( 'refresh_totals', true ); } } @@ -474,7 +513,6 @@ class WC_Cart extends WC_Legacy_Cart { $error = new WP_Error(); $product_qty_in_cart = $this->get_cart_item_quantities(); - // First stock check loop foreach ( $this->get_cart() as $cart_item_key => $values ) { $product = $values['data']; @@ -537,8 +575,8 @@ class WC_Cart extends WC_Legacy_Cart { /** * Gets and formats a list of cart item data + variations for display on the frontend. * - * @param array $cart_item - * @param bool $flat (default: false) + * @param array $cart_item Cart item object. + * @param bool $flat Should the data be returned flat or in a list. * @return string */ public function get_item_data( $cart_item, $flat = false ) { @@ -550,16 +588,15 @@ class WC_Cart extends WC_Legacy_Cart { foreach ( $cart_item['variation'] as $name => $value ) { $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) ); - // If this is a term slug, get the term's nice name if ( taxonomy_exists( $taxonomy ) ) { + // If this is a term slug, get the term's nice name. $term = get_term_by( 'slug', $value, $taxonomy ); if ( ! is_wp_error( $term ) && $term && $term->name ) { $value = $term->name; } $label = wc_attribute_label( $taxonomy ); - - // If this is a custom option slug, get the options name. } else { + // If this is a custom option slug, get the options name. $value = apply_filters( 'woocommerce_variation_option_name', $value ); $label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $cart_item['data'] ); } @@ -576,10 +613,10 @@ class WC_Cart extends WC_Legacy_Cart { } } - // Filter item data to allow 3rd parties to add more to the array + // Filter item data to allow 3rd parties to add more to the array. $item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $cart_item ); - // Format item data ready to display + // Format item data ready to display. foreach ( $item_data as $key => $data ) { // Set hidden to true to not display meta on cart. if ( ! empty( $data['hidden'] ) ) { @@ -590,8 +627,8 @@ class WC_Cart extends WC_Legacy_Cart { $item_data[ $key ]['display'] = ! empty( $data['display'] ) ? $data['display'] : $data['value']; } - // Output flat or in list format - if ( sizeof( $item_data ) > 0 ) { + // Output flat or in list format. + if ( count( $item_data ) > 0 ) { ob_start(); if ( $flat ) { @@ -628,32 +665,10 @@ class WC_Cart extends WC_Legacy_Cart { return apply_filters( 'woocommerce_cart_crosssell_ids', wp_parse_id_list( $cross_sells ), $this ); } - /** - * Gets the url to the cart page. - * - * @deprecated 2.5.0 in favor to wc_get_cart_url() - * - * @return string url to page - */ - public function get_cart_url() { - return wc_get_cart_url(); - } - - /** - * Gets the url to the checkout page. - * - * @deprecated 2.5.0 in favor to wc_get_checkout_url() - * - * @return string url to page - */ - public function get_checkout_url() { - return wc_get_checkout_url(); - } - /** * Gets the url to remove an item from the cart. * - * @param string $cart_item_key contains the id of the cart item + * @param string $cart_item_key contains the id of the cart item. * @return string url to page */ public function get_remove_url( $cart_item_key ) { @@ -664,7 +679,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Gets the url to re-add an item into the cart. * - * @param string $cart_item_key + * @param string $cart_item_key Cart item key to undo. * @return string url to page */ public function get_undo_url( $cart_item_key ) { @@ -703,7 +718,7 @@ class WC_Cart extends WC_Legacy_Cart { if ( $this->get_cart() ) { foreach ( $this->get_cart() as $key => $values ) { $cart_session[ $key ] = $values; - unset( $cart_session[ $key ]['data'] ); // Unset product object + unset( $cart_session[ $key ]['data'] ); // Unset product object. } } @@ -732,7 +747,6 @@ class WC_Cart extends WC_Legacy_Cart { public function get_taxes() { $taxes = array(); - // Merge foreach ( array_keys( $this->taxes + $this->shipping_taxes ) as $key ) { $taxes[ $key ] = ( isset( $this->shipping_taxes[ $key ] ) ? $this->shipping_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); } @@ -775,6 +789,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get all tax classes for items in the cart. + * * @return array */ public function get_cart_item_tax_classes() { @@ -809,16 +824,12 @@ class WC_Cart extends WC_Legacy_Cart { } } - /* - * Add to cart handling. - */ - /** * Check if product is in the cart and return cart item key. * * Cart item key will be unique based on the item and its properties, such as variations. * - * @param mixed id of product to find in the cart + * @param mixed $cart_id id of product to find in the cart. * @return string cart item key */ public function find_product_in_cart( $cart_id = false ) { @@ -833,16 +844,16 @@ class WC_Cart extends WC_Legacy_Cart { /** * Generate a unique ID for the cart item being added. * - * @param int $product_id - id of the product the key is being generated for - * @param int $variation_id of the product the key is being generated for - * @param array $variation data for the cart item - * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart + * @param int $product_id - id of the product the key is being generated for. + * @param int $variation_id of the product the key is being generated for. + * @param array $variation data for the cart item. + * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart. * @return string cart item key */ public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { $id_parts = array( $product_id ); - if ( $variation_id && 0 != $variation_id ) { + if ( $variation_id && 0 !== $variation_id ) { $id_parts[] = $variation_id; } @@ -872,46 +883,43 @@ class WC_Cart extends WC_Legacy_Cart { /** * Add a product to the cart. * - * @param int $product_id contains the id of the product to add to the cart - * @param int $quantity contains the quantity of the item to add - * @param int $variation_id - * @param array $variation attribute values - * @param array $cart_item_data extra cart item data we want to pass into the item + * @throws Exception To prevent adding to cart. + * @param int $product_id contains the id of the product to add to the cart. + * @param int $quantity contains the quantity of the item to add. + * @param int $variation_id ID of the variation being added to the cart. + * @param array $variation attribute values. + * @param array $cart_item_data extra cart item data we want to pass into the item. * @return string|bool $cart_item_key */ public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { - // Wrap in try catch so plugins can throw an exception to prevent adding to cart + // Wrap in try catch so plugins can throw an exception to prevent adding to cart. try { $product_id = absint( $product_id ); $variation_id = absint( $variation_id ); - // Ensure we don't add a variation to the cart directly by variation ID + // Ensure we don't add a variation to the cart directly by variation ID. if ( 'product_variation' === get_post_type( $product_id ) ) { $variation_id = $product_id; $product_id = wp_get_post_parent_id( $variation_id ); } - // Get the product $product_data = wc_get_product( $variation_id ? $variation_id : $product_id ); - - // Filter quantity being added to the cart before stock checks $quantity = apply_filters( 'woocommerce_add_to_cart_quantity', $quantity, $product_id ); - // Sanity check if ( $quantity <= 0 || ! $product_data || 'trash' === $product_data->get_status() ) { return false; } - // Load cart item data - may be added by other plugins + // Load cart item data - may be added by other plugins. $cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id ); - // Generate a ID based on product ID, variation ID, variation data, and other cart item data + // Generate a ID based on product ID, variation ID, variation data, and other cart item data. $cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data ); - // Find the cart item key in the existing cart + // Find the cart item key in the existing cart. $cart_item_key = $this->find_product_in_cart( $cart_id ); - // Force quantity to 1 if sold individually and check for existing item in cart + // Force quantity to 1 if sold individually and check for existing item in cart. if ( $product_data->is_sold_individually() ) { $quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data ); $found_in_cart = apply_filters( 'woocommerce_add_to_cart_sold_individually_found_in_cart', $cart_item_key && $this->cart_contents[ $cart_item_key ]['quantity'] > 0, $product_id, $variation_id, $cart_item_data, $cart_id ); @@ -922,12 +930,11 @@ class WC_Cart extends WC_Legacy_Cart { } } - // Check product is_purchasable if ( ! $product_data->is_purchasable() ) { throw new Exception( __( 'Sorry, this product cannot be purchased.', 'woocommerce' ) ); } - // Stock check - only check if we're managing stock and backorders are not allowed + // Stock check - only check if we're managing stock and backorders are not allowed. if ( ! $product_data->is_in_stock() ) { throw new Exception( sprintf( __( 'You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_name() ) ); } @@ -937,7 +944,7 @@ class WC_Cart extends WC_Legacy_Cart { 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 ) ) ); } - // Stock check - this time accounting for whats already in-cart + // Stock check - this time accounting for whats already in-cart. if ( $product_data->managing_stock() ) { $products_qty_in_cart = $this->get_cart_item_quantities(); @@ -951,14 +958,14 @@ class WC_Cart extends WC_Legacy_Cart { } } - // If cart_item_key is set, the item is already in the cart + // If cart_item_key is set, the item is already in the cart. if ( $cart_item_key ) { $new_quantity = $quantity + $this->cart_contents[ $cart_item_key ]['quantity']; $this->set_quantity( $cart_item_key, $new_quantity, false ); } else { $cart_item_key = $cart_id; - // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item + // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item. $this->cart_contents[ $cart_item_key ] = apply_filters( 'woocommerce_add_cart_item', array_merge( $cart_item_data, array( 'key' => $cart_item_key, 'product_id' => $product_id, @@ -989,7 +996,7 @@ class WC_Cart extends WC_Legacy_Cart { * Remove a cart item. * * @since 2.3.0 - * @param string $cart_item_key + * @param string $cart_item_key Cart item key to remove from the cart. * @return bool */ public function remove_cart_item( $cart_item_key ) { @@ -1014,7 +1021,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Restore a cart item. * - * @param string $cart_item_key + * @param string $cart_item_key Cart item key to restore to the cart. * @return bool */ public function restore_cart_item( $cart_item_key ) { @@ -1039,14 +1046,13 @@ class WC_Cart extends WC_Legacy_Cart { /** * Set the quantity for an item in the cart. * - * @param string $cart_item_key contains the id of the cart item - * @param int $quantity contains the quantity of the item - * @param bool $refresh_totals whether or not to calculate totals after setting the new qty - * + * @param string $cart_item_key contains the id of the cart item. + * @param int $quantity contains the quantity of the item. + * @param bool $refresh_totals whether or not to calculate totals after setting the new qty. * @return bool */ public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) { - if ( 0 == $quantity || $quantity < 0 ) { + if ( 0 === $quantity || $quantity < 0 ) { do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key ); unset( $this->cart_contents[ $cart_item_key ] ); } else { @@ -1062,14 +1068,10 @@ class WC_Cart extends WC_Legacy_Cart { return true; } - /* - * Cart Calculation Functions. - */ - /** * Reset cart totals to the defaults. Useful before running calculations. * - * @param bool $unset_session If true, the session data will be forced unset. + * @param bool $unset_session If true, the session data will be forced unset. * @access private */ private function reset( $unset_session = false ) { @@ -1082,21 +1084,6 @@ class WC_Cart extends WC_Legacy_Cart { do_action( 'woocommerce_cart_reset', $this, $unset_session ); } - /** - * Sort by subtotal. - * @param array $a - * @param array $b - * @return int - */ - private function sort_by_subtotal( $a, $b ) { - $first_item_subtotal = isset( $a['line_subtotal'] ) ? $a['line_subtotal'] : 0; - $second_item_subtotal = isset( $b['line_subtotal'] ) ? $b['line_subtotal'] : 0; - if ( $first_item_subtotal === $second_item_subtotal ) { - return 0; - } - return ( $first_item_subtotal < $second_item_subtotal ) ? 1 : -1; - } - /** * Get cart's owner. * @@ -1109,10 +1096,11 @@ class WC_Cart extends WC_Legacy_Cart { /** * Calculate totals for the items in the cart. + * + * @uses WC_Cart_Totals */ public function calculate_totals() { $this->reset(); - $this->coupons = $this->get_coupons(); do_action( 'woocommerce_before_calculate_totals', $this ); @@ -1121,197 +1109,7 @@ class WC_Cart extends WC_Legacy_Cart { return; } - $tax_rates = array(); - $shop_tax_rates = array(); - $cart = $this->get_cart(); - - // Order cart items by price so coupon logic is 'fair' for customers and not based on order added to cart. - uasort( $cart, apply_filters( 'woocommerce_sort_by_subtotal_callback', array( $this, 'sort_by_subtotal' ) ) ); - - /** - * Calculate totals for items. - */ - foreach ( $cart as $cart_item_key => $values ) { - - $product = $values['data']; - - // Prices - $base_price = $product->get_price(); - $line_price = $product->get_price() * $values['quantity']; - - // Tax data - $taxes = array(); - $discounted_taxes = array(); - - /** - * No tax to calculate. - */ - if ( ! $product->is_taxable() ) { - - // Discounted Price (price with any pre-tax discounts applied) - $discounted_price = $this->get_discounted_price( $values, $base_price, true ); - $line_subtotal_tax = 0; - $line_subtotal = $line_price; - $line_tax = 0; - $line_total = round( $discounted_price * $values['quantity'], wc_get_rounding_precision() ); - - /** - * Prices include tax. - */ - } elseif ( $this->prices_include_tax ) { - - if ( empty( $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ] ) ) { - $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ] = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); - } - - if ( empty( $tax_rates[ $product->get_tax_class() ] ) ) { - $tax_rates[ $product->get_tax_class() ] = WC_Tax::get_rates( $product->get_tax_class() ); - } - - $base_tax_rates = $shop_tax_rates[ $product->get_tax_class( 'unfiltered' ) ]; - $item_tax_rates = $tax_rates[ $product->get_tax_class() ]; - - /** - * ADJUST TAX - Calculations when base tax is not equal to the item tax. - * - * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations. - * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes. - * This feature is experimental @since 2.4.7 and may change in the future. Use at your risk. - */ - if ( $item_tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { - - // Work out a new base price without the shop's base tax - $taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true, true ); - - // Now we have a new item price (excluding TAX) - $line_subtotal = round( $line_price - array_sum( $taxes ), wc_get_rounding_precision() ); - $taxes = WC_Tax::calc_tax( $line_subtotal, $item_tax_rates ); - $line_subtotal_tax = array_sum( $taxes ); - - // Adjusted price (this is the price including the new tax rate) - $adjusted_price = ( $line_subtotal + $line_subtotal_tax ) / $values['quantity']; - - // Apply discounts and get the discounted price FOR A SINGLE ITEM - $discounted_price = $this->get_discounted_price( $values, $adjusted_price, true ); - - // Convert back to line price - $discounted_line_price = $discounted_price * $values['quantity']; - - // Now use rounded line price to get taxes. - $discounted_taxes = WC_Tax::calc_tax( $discounted_line_price, $item_tax_rates, true ); - $line_tax = array_sum( $discounted_taxes ); - $line_total = $discounted_line_price - $line_tax; - - /** - * Regular tax calculation (customer inside base and the tax class is unmodified. - */ - } else { - - // Work out a new base price without the item tax - $taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates, true ); - - // Now we have a new item price (excluding TAX) - $line_subtotal = $line_price - array_sum( $taxes ); - $line_subtotal_tax = array_sum( $taxes ); - - // Calc prices and tax (discounted) - $discounted_price = $this->get_discounted_price( $values, $base_price, true ); - - // Convert back to line price - $discounted_line_price = $discounted_price * $values['quantity']; - - // Now use rounded line price to get taxes. - $discounted_taxes = WC_Tax::calc_tax( $discounted_line_price, $item_tax_rates, true ); - $line_tax = array_sum( $discounted_taxes ); - $line_total = $discounted_line_price - $line_tax; - } - - // Tax rows - merge the totals we just got - foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) { - $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); - } - - /** - * Prices exclude tax. - */ - } else { - if ( empty( $tax_rates[ $product->get_tax_class() ] ) ) { - $tax_rates[ $product->get_tax_class() ] = WC_Tax::get_rates( $product->get_tax_class() ); - } - - $item_tax_rates = $tax_rates[ $product->get_tax_class() ]; - - // Work out a new base price without the shop's base tax - $taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates ); - - // Now we have the item price (excluding TAX) - $line_subtotal = $line_price; - $line_subtotal_tax = array_sum( $taxes ); - - // Now calc product rates - $discounted_price = $this->get_discounted_price( $values, $base_price, true ); - $discounted_taxes = WC_Tax::calc_tax( $discounted_price * $values['quantity'], $item_tax_rates ); - $discounted_tax_amount = array_sum( $discounted_taxes ); - $line_tax = $discounted_tax_amount; - $line_total = $discounted_price * $values['quantity']; - - // Tax rows - merge the totals we just got - foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) { - $this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); - } - } - - // Cart contents total is based on discounted prices and is used for the final total calculation - $this->cart_contents_total += $line_total; - - /** - * Store costs + taxes for lines. For tax inclusive prices, we do some extra rounding logic so the stored - * values "add up" when viewing the order in admin. This does have the disadvantage of not being able to - * recalculate the tax total/subtotal accurately in the future, but it does ensure the data looks correct. - * - * Tax exclusive prices are not affected. - */ - if ( ! $product->is_taxable() || $this->prices_include_tax ) { - $this->cart_contents[ $cart_item_key ]['line_total'] = round( $line_total + $line_tax - wc_round_tax_total( $line_tax ), $this->dp ); - $this->cart_contents[ $cart_item_key ]['line_subtotal'] = round( $line_subtotal + $line_subtotal_tax - wc_round_tax_total( $line_subtotal_tax ), $this->dp ); - $this->cart_contents[ $cart_item_key ]['line_tax'] = wc_round_tax_total( $line_tax ); - $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = wc_round_tax_total( $line_subtotal_tax ); - $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 ) ); - } else { - $this->cart_contents[ $cart_item_key ]['line_total'] = $line_total; - $this->cart_contents[ $cart_item_key ]['line_subtotal'] = $line_subtotal; - $this->cart_contents[ $cart_item_key ]['line_tax'] = $line_tax; - $this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $line_subtotal_tax; - $this->cart_contents[ $cart_item_key ]['line_tax_data'] = array( 'total' => $discounted_taxes, 'subtotal' => $taxes ); - } - } - - // Only calculate the grand total + shipping if on the cart/checkout - if ( is_checkout() || is_cart() || defined( 'WOOCOMMERCE_CHECKOUT' ) || defined( 'WOOCOMMERCE_CART' ) ) { - - // Calculate the Shipping. - $local_pickup_methods = apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) ); - $had_local_pickup = 0 < count( array_intersect( wc_get_chosen_shipping_method_ids(), $local_pickup_methods ) ); - $this->calculate_shipping(); - $has_local_pickup = 0 < count( array_intersect( wc_get_chosen_shipping_method_ids(), $local_pickup_methods ) ); - - // If methods changed and local pickup is selected, we need to do a recalculation of taxes. - if ( true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && $had_local_pickup !== $has_local_pickup ) { - return $this->calculate_totals(); - } - - new WC_Cart_Totals( $this ); - - } else { - - // Set tax total to sum of all tax rows - $this->tax_total = WC_Tax::get_tax_total( $this->taxes ); - - // VAT exemption done at this point - so all totals are correct before exemption - if ( $this->get_customer()->get_is_vat_exempt() ) { - $this->remove_taxes(); - } - } + new WC_Cart_Totals( $this ); do_action( 'woocommerce_after_calculate_totals', $this ); @@ -1368,7 +1166,7 @@ class WC_Cart extends WC_Legacy_Cart { * Given a set of packages with rates, get the chosen ones only. * * @since 3.2.0 - * @param array $calculated_shipping_packages + * @param array $calculated_shipping_packages Array of packages. * @return array */ protected function get_chosen_shipping_methods( $calculated_shipping_packages = array() ) { @@ -1387,7 +1185,7 @@ class WC_Cart extends WC_Legacy_Cart { * Filter items needing shipping callback. * * @since 3.0.0 - * @param array $item + * @param array $item Item to check for shipping. * @return bool */ protected function filter_items_needing_shipping( $item ) { @@ -1471,14 +1269,7 @@ class WC_Cart extends WC_Legacy_Cart { * @return bool */ public function needs_shipping_address() { - - $needs_shipping_address = false; - - if ( $this->needs_shipping() === true && ! wc_ship_to_billing_address_only() ) { - $needs_shipping_address = true; - } - - return apply_filters( 'woocommerce_cart_needs_shipping_address', $needs_shipping_address ); + return apply_filters( 'woocommerce_cart_needs_shipping_address', $this->needs_shipping() === true && ! wc_ship_to_billing_address_only() ); } /** @@ -1502,17 +1293,6 @@ class WC_Cart extends WC_Legacy_Cart { return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true ); } - /** - * Sees if we need a shipping address. - * - * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only() - * - * @return bool - */ - public function ship_to_billing_address_only() { - return wc_ship_to_billing_address_only(); - } - /** * Gets the shipping total (after calculation). * @@ -1522,9 +1302,7 @@ class WC_Cart extends WC_Legacy_Cart { if ( isset( $this->shipping_total ) ) { if ( $this->shipping_total > 0 ) { - // Display varies depending on settings if ( 'excl' === $this->tax_display_cart ) { - $return = wc_price( $this->shipping_total ); if ( $this->shipping_tax_total > 0 && $this->prices_include_tax ) { @@ -1532,9 +1310,7 @@ class WC_Cart extends WC_Legacy_Cart { } return $return; - } else { - $return = wc_price( $this->shipping_total + $this->shipping_tax_total ); if ( $this->shipping_tax_total > 0 && ! $this->prices_include_tax ) { @@ -1542,20 +1318,14 @@ class WC_Cart extends WC_Legacy_Cart { } return $return; - } } else { return __( 'Free!', 'woocommerce' ); } } - return ''; } - /* - * Coupons/Discount related functions. - */ - /** * Check for user coupons (now that we have billing email). If a coupon is invalid, add an error. * @@ -1563,7 +1333,7 @@ class WC_Cart extends WC_Legacy_Cart { * 1. Where a list of customer emails are set (limits coupon usage to those defined). * 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email). * - * @param array $posted + * @param array $posted Post data. */ public function check_customer_coupons( $posted ) { if ( ! empty( $this->applied_coupons ) ) { @@ -1572,8 +1342,8 @@ class WC_Cart extends WC_Legacy_Cart { if ( $coupon->is_valid() ) { - // Limit to defined email addresses - if ( is_array( $coupon->get_email_restrictions() ) && sizeof( $coupon->get_email_restrictions() ) > 0 ) { + // Limit to defined email addresses. + if ( is_array( $coupon->get_email_restrictions() ) && count( $coupon->get_email_restrictions() ) > 0 ) { $check_emails = array(); if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); @@ -1582,18 +1352,16 @@ class WC_Cart extends WC_Legacy_Cart { $check_emails[] = $posted['billing_email']; $check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) ); - if ( 0 == sizeof( array_intersect( $check_emails, $coupon->get_email_restrictions() ) ) ) { + if ( 0 === count( array_intersect( $check_emails, $coupon->get_email_restrictions() ) ) ) { $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ); - - // Remove the coupon $this->remove_coupon( $code ); - // Flag totals for refresh + // Flag totals for refresh. WC()->session->set( 'refresh_totals', true ); } } - // Usage limits per user - check against billing and user email and user ID + // Usage limits per user - check against billing and user email and user ID. if ( $coupon->get_usage_limit_per_user() > 0 ) { $check_emails = array(); $used_by = $coupon->get_used_by(); @@ -1601,28 +1369,26 @@ class WC_Cart extends WC_Legacy_Cart { if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); $check_emails[] = sanitize_email( $current_user->user_email ); - $usage_count = sizeof( array_keys( $used_by, get_current_user_id() ) ); + $usage_count = count( array_keys( $used_by, get_current_user_id(), true ) ); } else { $check_emails[] = sanitize_email( $posted['billing_email'] ); $user = get_user_by( 'email', $posted['billing_email'] ); if ( $user ) { - $usage_count = sizeof( array_keys( $used_by, $user->ID ) ); + $usage_count = count( array_keys( $used_by, $user->ID, true ) ); } else { $usage_count = 0; } } foreach ( $check_emails as $check_email ) { - $usage_count = $usage_count + sizeof( array_keys( $used_by, $check_email ) ); + $usage_count = $usage_count + count( array_keys( $used_by, $check_email, true ) ); } if ( $usage_count >= $coupon->get_usage_limit_per_user() ) { $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ); - - // Remove the coupon $this->remove_coupon( $code ); - // Flag totals for refresh + // Flag totals for refresh. WC()->session->set( 'refresh_totals', true ); } } @@ -1633,18 +1399,19 @@ class WC_Cart extends WC_Legacy_Cart { /** * Returns whether or not a discount has been applied. - * @param string $coupon_code + * + * @param string $coupon_code Coupon code to check. * @return bool */ public function has_discount( $coupon_code = '' ) { - return $coupon_code ? in_array( wc_format_coupon_code( $coupon_code ), $this->applied_coupons ) : sizeof( $this->applied_coupons ) > 0; + return $coupon_code ? in_array( wc_format_coupon_code( $coupon_code ), $this->applied_coupons, true ) : count( $this->applied_coupons ) > 0; } /** * Applies a coupon code passed to the method. * - * @param string $coupon_code - The code to apply - * @return bool True if the coupon is applied, false if it does not exist or cannot be applied + * @param string $coupon_code - The code to apply. + * @return bool True if the coupon is applied, false if it does not exist or cannot be applied. */ public function add_discount( $coupon_code ) { // Coupons are globally disabled. @@ -1682,7 +1449,7 @@ class WC_Cart extends WC_Legacy_Cart { $coupons_to_keep = apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons ); foreach ( $this->applied_coupons as $applied_coupon ) { - $keep_key = array_search( $applied_coupon, $coupons_to_keep ); + $keep_key = array_search( $applied_coupon, $coupons_to_keep, true ); if ( false === $keep_key ) { $this->remove_coupon( $applied_coupon ); } else { @@ -1734,8 +1501,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get array of applied coupon objects and codes. * - * @param null $deprecated - * + * @param null $deprecated No longer used. * @return array of applied coupons */ public function get_coupons( $deprecated = null ) { @@ -1764,8 +1530,9 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get the discount amount for a used coupon. - * @param string $code coupon code - * @param bool $ex_tax inc or ex tax + * + * @param string $code coupon code. + * @param bool $ex_tax inc or ex tax. * @return float discount amount */ public function get_coupon_discount_amount( $code, $ex_tax = true ) { @@ -1780,8 +1547,8 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get the discount tax amount for a used coupon (for tax inclusive prices). - * @param string $code coupon code - * @param bool inc or ex tax + * + * @param string $code coupon code. * @return float discount amount */ public function get_coupon_discount_tax_amount( $code ) { @@ -1791,7 +1558,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax. * - * @param null $deprecated + * @param null $deprecated No longer used. */ public function remove_coupons( $deprecated = null ) { $this->applied_coupons = $this->coupon_discount_amounts = $this->coupon_discount_tax_amounts = $this->coupon_applied_count = array(); @@ -1802,18 +1569,18 @@ class WC_Cart extends WC_Legacy_Cart { /** * Remove a single coupon by code. - * @param string $coupon_code Code of the coupon to remove + * + * @param string $coupon_code Code of the coupon to remove. * @return bool */ public function remove_coupon( $coupon_code ) { - // Coupons are globally disabled + // Coupons are globally disabled. if ( ! wc_coupons_enabled() ) { return false; } - // Get the coupon $coupon_code = wc_format_coupon_code( $coupon_code ); - $position = array_search( $coupon_code, $this->applied_coupons ); + $position = array_search( $coupon_code, $this->applied_coupons, true ); if ( false !== $position ) { unset( $this->applied_coupons[ $position ] ); @@ -1826,76 +1593,6 @@ class WC_Cart extends WC_Legacy_Cart { return true; } - /** - * Function to apply discounts to a product and get the discounted price (before tax is applied). - * - * @param mixed $values - * @param mixed $price - * @param bool $add_totals (default: false) - * @return float price - */ - public function get_discounted_price( $values, $price, $add_totals = false ) { - if ( ! $price ) { - return $price; - } - - $undiscounted_price = $price; - - if ( ! empty( $this->coupons ) ) { - $product = $values['data']; - - foreach ( $this->coupons as $code => $coupon ) { - if ( $coupon->is_valid() && ( $coupon->is_valid_for_product( $product, $values ) || $coupon->is_valid_for_cart() ) ) { - $discount_amount = $coupon->get_discount_amount( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ? $price : $undiscounted_price, $values, true ); - $discount_amount = min( $price, $discount_amount ); - $price = max( $price - $discount_amount, 0 ); - - // Store the totals for DISPLAY in the cart. - if ( $add_totals ) { - $total_discount = $discount_amount * $values['quantity']; - $total_discount_tax = 0; - - if ( wc_tax_enabled() && $product->is_taxable() ) { - $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); - $taxes = WC_Tax::calc_tax( $discount_amount, $tax_rates, $this->prices_include_tax ); - $total_discount_tax = WC_Tax::get_tax_total( $taxes ) * $values['quantity']; - $total_discount = $this->prices_include_tax ? $total_discount - $total_discount_tax : $total_discount; - $this->discount_cart_tax += $total_discount_tax; - } - - $this->discount_cart += $total_discount; - $this->increase_coupon_discount_amount( $code, $total_discount, $total_discount_tax ); - $this->increase_coupon_applied_count( $code, $values['quantity'] ); - } - } - - // If the price is 0, we can stop going through coupons because there is nothing more to discount for this product. - if ( 0 >= $price ) { - break; - } - } - } - - return apply_filters( 'woocommerce_get_discounted_price', $price, $values, $this ); - } - - /** - * Store how much discount each coupon grants. - * - * @access private - * @param string $code - * @param double $amount - * @param double $tax - */ - private function increase_coupon_discount_amount( $code, $amount, $tax ) { - $this->coupon_discount_amounts[ $code ] = isset( $this->coupon_discount_amounts[ $code ] ) ? $this->coupon_discount_amounts[ $code ] + $amount : $amount; - $this->coupon_discount_tax_amounts[ $code ] = isset( $this->coupon_discount_tax_amounts[ $code ] ) ? $this->coupon_discount_tax_amounts[ $code ] + $tax : $tax; - } - - /* - * Fees API to add additional costs to orders. - */ - /** * Add additional fee to the cart. * @@ -1912,12 +1609,11 @@ class WC_Cart extends WC_Legacy_Cart { * @param string $tax_class The tax class for the fee if taxable. A blank string is standard tax class. (default: ''). */ public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) { - $new_fee_id = sanitize_title( $name ); // Only add each fee once. foreach ( $this->fees as $fee ) { - if ( $fee->id == $new_fee_id ) { + if ( $fee->id === $new_fee_id ) { return; } } @@ -1946,31 +1642,27 @@ class WC_Cart extends WC_Legacy_Cart { * Calculate fees. */ public function calculate_fees() { - // Reset fees before calculation + // Reset fees before calculation. $this->fee_total = 0; $this->fees = array(); - // Fire an action where developers can add their fees + // Fire an action where developers can add their fees. do_action( 'woocommerce_cart_calculate_fees', $this ); - // If fees were added, total them and calculate tax + // If fees were added, total them and calculate tax. if ( ! empty( $this->fees ) ) { foreach ( $this->fees as $fee_key => $fee ) { $this->fee_total += $fee->amount; if ( $fee->taxable ) { - // Get tax rates $tax_rates = WC_Tax::get_rates( $fee->tax_class ); $fee_taxes = WC_Tax::calc_tax( $fee->amount, $tax_rates, false ); if ( ! empty( $fee_taxes ) ) { - // Set the tax total for this fee $this->fees[ $fee_key ]->tax = array_sum( $fee_taxes ); - - // Set tax data - Since 2.2 $this->fees[ $fee_key ]->tax_data = $fee_taxes; - // Tax rows - merge the totals we just got + // Tax rows - merge the totals we just got. foreach ( array_keys( $this->taxes + $fee_taxes ) as $key ) { $this->taxes[ $key ] = ( isset( $fee_taxes[ $key ] ) ? $fee_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 ); } @@ -1980,10 +1672,6 @@ class WC_Cart extends WC_Legacy_Cart { } } - /* - * Get Formatted Totals. - */ - /** * Gets the order total (after calculation). * @@ -2009,7 +1697,6 @@ class WC_Cart extends WC_Legacy_Cart { /** * Gets the cart contents total (after calculation). * - * @todo deprecate? It's unused. * @return string formatted price */ public function get_cart_total() { @@ -2025,35 +1712,28 @@ class WC_Cart extends WC_Legacy_Cart { /** * Gets the sub total (after calculation). * - * @param bool $compound whether to include compound taxes + * @param bool $compound whether to include compound taxes. * @return string formatted price */ public function get_cart_subtotal( $compound = false ) { - // If the cart has compound tax, we want to show the subtotal as - // cart + shipping + non-compound taxes (after discount) + /** + * If the cart has compound tax, we want to show the subtotal as cart + shipping + non-compound taxes (after discount). + */ if ( $compound ) { - $cart_subtotal = wc_price( $this->cart_contents_total + $this->shipping_total + $this->get_taxes_total( false, false ) ); - // Otherwise we show cart items totals only (before discount) + } elseif ( 'excl' === $this->tax_display_cart ) { + $cart_subtotal = wc_price( $this->subtotal_ex_tax ); + + if ( $this->tax_total > 0 && $this->prices_include_tax ) { + $cart_subtotal .= ' ' . WC()->countries->ex_tax_or_vat() . ''; + } } else { + $cart_subtotal = wc_price( $this->subtotal ); - // Display varies depending on settings - if ( 'excl' === $this->tax_display_cart ) { - - $cart_subtotal = wc_price( $this->subtotal_ex_tax ); - - if ( $this->tax_total > 0 && $this->prices_include_tax ) { - $cart_subtotal .= ' ' . WC()->countries->ex_tax_or_vat() . ''; - } - } else { - - $cart_subtotal = wc_price( $this->subtotal ); - - if ( $this->tax_total > 0 && ! $this->prices_include_tax ) { - $cart_subtotal .= ' ' . WC()->countries->inc_tax_or_vat() . ''; - } + if ( $this->tax_total > 0 && ! $this->prices_include_tax ) { + $cart_subtotal .= ' ' . WC()->countries->inc_tax_or_vat() . ''; } } @@ -2063,7 +1743,7 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get the product row price per item. * - * @param WC_Product $product + * @param WC_Product $product Product object. * @return string formatted price */ public function get_product_price( $product ) { @@ -2082,16 +1762,14 @@ class WC_Cart extends WC_Legacy_Cart { * * When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate. * - * @param WC_Product $product - * @param int $quantity + * @param WC_Product $product Product object. + * @param int $quantity Quantity being purchased. * @return string formatted price */ public function get_product_subtotal( $product, $quantity ) { - $price = $product->get_price(); - $taxable = $product->is_taxable(); + $price = $product->get_price(); - // Taxable - if ( $taxable ) { + if ( $product->is_taxable() ) { if ( 'excl' === $this->tax_display_cart ) { @@ -2110,13 +1788,9 @@ class WC_Cart extends WC_Legacy_Cart { $product_subtotal .= ' ' . WC()->countries->inc_tax_or_vat() . ''; } } - - // Non-taxable } else { - $row_price = $price * $quantity; $product_subtotal = wc_price( $row_price ); - } return apply_filters( 'woocommerce_cart_product_subtotal', $product_subtotal, $product, $quantity, $this ); @@ -2135,7 +1809,8 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get a tax amount. - * @param string $tax_rate_id + * + * @param string $tax_rate_id ID of the tax rate to get taxes for. * @return float amount */ public function get_tax_amount( $tax_rate_id ) { @@ -2144,7 +1819,8 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get a tax amount. - * @param string $tax_rate_id + * + * @param string $tax_rate_id ID of the tax rate to get taxes for. * @return float amount */ public function get_shipping_tax_amount( $tax_rate_id ) { @@ -2154,8 +1830,8 @@ class WC_Cart extends WC_Legacy_Cart { /** * Get tax row amounts with or without compound taxes includes. * - * @param bool $compound True if getting compound taxes - * @param bool $display True if getting total to display + * @param bool $compound True if getting compound taxes. + * @param bool $display True if getting total to display. * @return float price */ public function get_taxes_total( $compound = true, $display = true ) { @@ -2202,68 +1878,6 @@ class WC_Cart extends WC_Legacy_Cart { * @return mixed formatted price or false if there are none */ public function get_total_discount() { - if ( $this->get_cart_discount_total() ) { - $total_discount = wc_price( $this->get_cart_discount_total() ); - } else { - $total_discount = false; - } - return apply_filters( 'woocommerce_cart_total_discount', $total_discount, $this ); - } - - /** - * Gets the total (product) discount amount - these are applied before tax. - * - * @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required. - * @return mixed formatted price or false if there are none - */ - public function get_discounts_before_tax() { - wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' ); - if ( $this->get_cart_discount_total() ) { - $discounts_before_tax = wc_price( $this->get_cart_discount_total() ); - } else { - $discounts_before_tax = false; - } - return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this ); - } - - /** - * Get the total of all order discounts (after tax discounts). - * - * @deprecated Order discounts (after tax) removed in 2.3 - * @return int - */ - public function get_order_discount_total() { - wc_deprecated_function( 'get_order_discount_total', '2.3' ); - return 0; - } - - /** - * Function to apply cart discounts after tax. - * @deprecated Coupons can not be applied after tax - * - * @param $values - * @param $price - */ - public function apply_cart_discounts_after_tax( $values, $price ) { - wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' ); - } - - /** - * Function to apply product discounts after tax. - * @deprecated Coupons can not be applied after tax - * - * @param $values - * @param $price - */ - public function apply_product_discounts_after_tax( $values, $price ) { - wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' ); - } - - /** - * Gets the order discount amount - these are applied after tax. - * @deprecated Coupons can not be applied after tax - */ - public function get_discounts_after_tax() { - wc_deprecated_function( 'get_discounts_after_tax', '2.3' ); + return apply_filters( 'woocommerce_cart_total_discount', $this->get_cart_discount_total() ? wc_price( $this->get_cart_discount_total() ) : false, $this ); } } diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 14d7517c9d1..5b77db9f751 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -174,10 +174,10 @@ class WC_Discounts { * * @since 3.2.0 * @param object $item Get data for this item. - * @return float + * @return int */ public function get_discounted_price_in_cents( $item ) { - return $item->price - $this->get_discount( $item->key, true ); + return absint( $item->price - $this->get_discount( $item->key, true ) ); } /** @@ -388,6 +388,7 @@ class WC_Discounts { */ protected function apply_coupon_percent( $coupon, $items_to_apply ) { $total_discount = 0; + $cart_total = 0; foreach ( $items_to_apply as $item ) { // Find out how much price is available to discount for the item. @@ -396,14 +397,25 @@ class WC_Discounts { // Get the price we actually want to discount, based on settings. $price_to_discount = ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $item->price: $discounted_price; + // Total up. + $cart_total += $price_to_discount; + // Run coupon calculations. - $discount = $coupon->get_amount() * ( $price_to_discount / 100 ); + $discount = floor( $price_to_discount * ( $coupon->get_amount() / 100 ) ); $discount = min( $discounted_price, apply_filters( 'woocommerce_coupon_get_discount_amount', $discount, $price_to_discount, $item->object, false, $coupon ) ); $total_discount += $discount; // Store code and discount amount per item. $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount; } + + // Work out how much discount would have been given to the cart has a whole and compare to what was discounted on all line items. + $cart_total_discount = wc_cart_round_discount( $cart_total * ( $coupon->get_amount() / 100 ), 0 ); + + if ( $total_discount < $cart_total_discount ) { + $total_discount += $this->apply_coupon_remainder( $coupon, $items_to_apply, $cart_total_discount - $total_discount ); + } + return $total_discount; } @@ -449,26 +461,26 @@ class WC_Discounts { */ protected function apply_coupon_fixed_cart( $coupon, $items_to_apply, $amount = null ) { $total_discount = 0; - $amount = $amount ? $amount: wc_add_number_precision( $coupon->get_amount() ); + $amount = $amount ? $amount : wc_add_number_precision( $coupon->get_amount() ); $items_to_apply = array_filter( $items_to_apply, array( $this, 'filter_products_with_price' ) ); if ( ! $item_count = array_sum( wp_list_pluck( $items_to_apply, 'quantity' ) ) ) { return $total_discount; } - $per_item_discount = floor( $amount / $item_count ); // round it down to the nearest cent. + $per_item_discount = absint( $amount / $item_count ); // round it down to the nearest cent. if ( $per_item_discount > 0 ) { - $total_discounted = $this->apply_coupon_fixed_product( $coupon, $items_to_apply, $per_item_discount ); + $total_discount = $this->apply_coupon_fixed_product( $coupon, $items_to_apply, $per_item_discount ); /** * If there is still discount remaining, repeat the process. */ - if ( $total_discounted > 0 && $total_discounted < $amount ) { - $total_discounted = $total_discounted + $this->apply_coupon_fixed_cart( $coupon, $items_to_apply, $amount - $total_discounted ); + if ( $total_discount > 0 && $total_discount < $amount ) { + $total_discount += $this->apply_coupon_fixed_cart( $coupon, $items_to_apply, $amount - $total_discount ); } } elseif ( $amount > 0 ) { - $total_discounted = $this->apply_coupon_fixed_cart_remainder( $coupon, $items_to_apply, $amount ); + $total_discount += $this->apply_coupon_remainder( $coupon, $items_to_apply, $amount ); } return $total_discount; } @@ -483,7 +495,7 @@ class WC_Discounts { * @param int $amount Fixed discount amount to apply. * @return int Total discounted. */ - protected function apply_coupon_fixed_cart_remainder( $coupon, $items_to_apply, $amount ) { + protected function apply_coupon_remainder( $coupon, $items_to_apply, $amount ) { $total_discount = 0; foreach ( $items_to_apply as $item ) { diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index 0708a7ee6fc..1ca61f52e45 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -405,7 +405,6 @@ final class WooCommerce { include_once( WC_ABSPATH . 'includes/class-wc-frontend-scripts.php' ); // Frontend Scripts. include_once( WC_ABSPATH . 'includes/class-wc-form-handler.php' ); // Form Handlers. include_once( WC_ABSPATH . 'includes/class-wc-cart.php' ); // The main cart class. - include_once( WC_ABSPATH . 'includes/class-wc-cart-item.php' ); include_once( WC_ABSPATH . 'includes/class-wc-tax.php' ); // Tax class. include_once( WC_ABSPATH . 'includes/class-wc-shipping-zones.php' ); // Shipping Zones class. include_once( WC_ABSPATH . 'includes/class-wc-customer.php' ); // Customer class. diff --git a/includes/legacy/class-wc-legacy-cart.php b/includes/legacy/class-wc-legacy-cart.php index 24738857ad4..5a806b6b019 100644 --- a/includes/legacy/class-wc-legacy-cart.php +++ b/includes/legacy/class-wc-legacy-cart.php @@ -23,22 +23,128 @@ abstract class WC_Legacy_Cart { /** * Contains an array of coupon usage counts after they have been applied. * + * @deprecated 3.2.0 * @var array */ public $coupon_applied_count = array(); /** - * Store how many times each coupon is applied to cart/items. + * Function to apply discounts to a product and get the discounted price (before tax is applied). * - * @deprecated 3.2.0 - * @access private - * @param string $code - * @param int $count + * @deprecated Calculation and coupon logic is handled in WC_Cart_Totals. + * @param mixed $values Cart item. + * @param mixed $price Price of item. + * @param bool $add_totals Legacy. + * @return float price */ - protected function increase_coupon_applied_count( $code, $count = 1 ) { - if ( empty( $this->coupon_applied_count[ $code ] ) ) { - $this->coupon_applied_count[ $code ] = 0; + public function get_discounted_price( $values, $price, $add_totals = false ) { + wc_deprecated_function( 'WC_Cart::get_discounted_price', '3.2', '' ); + + $cart_item_key = $values['key']; + $cart_item = $this->cart_contents[ $cart_item_key ]; + + return $cart_item->get_line_total(); + } + + /** + * Gets the url to the cart page. + * + * @deprecated 2.5.0 in favor to wc_get_cart_url() + * @return string url to page + */ + public function get_cart_url() { + wc_deprecated_function( 'WC_Cart::get_cart_url', '2.5', 'wc_get_cart_url' ); + return wc_get_cart_url(); + } + + /** + * Gets the url to the checkout page. + * + * @deprecated 2.5.0 in favor to wc_get_checkout_url() + * @return string url to page + */ + public function get_checkout_url() { + wc_deprecated_function( 'WC_Cart::get_checkout_url', '2.5', 'wc_get_checkout_url' ); + return wc_get_checkout_url(); + } + + /** + * Sees if we need a shipping address. + * + * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only() + * @return bool + */ + public function ship_to_billing_address_only() { + wc_deprecated_function( 'WC_Cart::ship_to_billing_address_only', '2.5', 'wc_ship_to_billing_address_only' ); + return wc_ship_to_billing_address_only(); + } + + /** + * Coupons enabled function. Filterable. + * + * @deprecated 2.5.0 in favor to wc_coupons_enabled() + * @return bool + */ + public function coupons_enabled() { + return wc_coupons_enabled(); + } + + /** + * Gets the total (product) discount amount - these are applied before tax. + * + * @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required. + * @return mixed formatted price or false if there are none. + */ + public function get_discounts_before_tax() { + wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' ); + if ( $this->get_cart_discount_total() ) { + $discounts_before_tax = wc_price( $this->get_cart_discount_total() ); + } else { + $discounts_before_tax = false; } - $this->coupon_applied_count[ $code ] += $count; + return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this ); + } + + /** + * Get the total of all order discounts (after tax discounts). + * + * @deprecated Order discounts (after tax) removed in 2.3. + * @return int + */ + public function get_order_discount_total() { + wc_deprecated_function( 'get_order_discount_total', '2.3' ); + return 0; + } + + /** + * Function to apply cart discounts after tax. + * + * @deprecated Coupons can not be applied after tax. + * @param $values + * @param $price + */ + public function apply_cart_discounts_after_tax( $values, $price ) { + wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' ); + } + + /** + * Function to apply product discounts after tax. + * + * @deprecated Coupons can not be applied after tax. + * + * @param $values + * @param $price + */ + public function apply_product_discounts_after_tax( $values, $price ) { + wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' ); + } + + /** + * Gets the order discount amount - these are applied after tax. + * + * @deprecated Coupons can not be applied after tax. + */ + public function get_discounts_after_tax() { + wc_deprecated_function( 'get_discounts_after_tax', '2.3' ); } } diff --git a/tests/unit-tests/cart/cart.php b/tests/unit-tests/cart/cart.php index a00c180c622..5b82efb4c41 100644 --- a/tests/unit-tests/cart/cart.php +++ b/tests/unit-tests/cart/cart.php @@ -150,6 +150,75 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { WC_Helper_Coupon::delete_coupon( $coupon->get_id() ); } + /** + * Test that calculation rounding is done correctly with and without taxes. + * + * @see https://github.com/woocommerce/woocommerce/issues/16305 + * @since 3.2 + */ + public function test_discount_cart_rounding() { + global $wpdb; + + # Test with no taxes. + WC()->cart->empty_cart(); + WC()->cart->remove_coupons(); + + $product = new WC_Product_Simple; + $product->set_regular_price( 51.86 ); + $product->save(); + + $coupon = new WC_Coupon; + $coupon->set_code( 'testpercent' ); + $coupon->set_discount_type( 'percent' ); + $coupon->set_amount( 40 ); + $coupon->save(); + + WC()->cart->add_to_cart( $product->get_id(), 1 ); + WC()->cart->add_discount( $coupon->get_code() ); + + WC()->cart->calculate_totals(); + $cart_item = current( WC()->cart->get_cart() ); + $this->assertEquals( '31.12', number_format( $cart_item['line_total'], 2, '.', '' ) ); + + // Clean up. + WC()->cart->empty_cart(); + WC()->cart->remove_coupons(); + + # Test with taxes. + update_option( 'woocommerce_prices_include_tax', 'no' ); + update_option( 'woocommerce_calc_taxes', 'yes' ); + + $tax_rate = array( + 'tax_rate_country' => '', + 'tax_rate_state' => '', + 'tax_rate' => '8.2500', + 'tax_rate_name' => 'TAX', + 'tax_rate_priority' => '1', + 'tax_rate_compound' => '0', + 'tax_rate_shipping' => '1', + 'tax_rate_order' => '1', + 'tax_rate_class' => '', + ); + WC_Tax::_insert_tax_rate( $tax_rate ); + + WC()->cart->add_to_cart( $product->get_id(), 1 ); + WC()->cart->add_discount( $coupon->get_code() ); + + WC()->cart->calculate_totals(); + $cart_item = current( WC()->cart->get_cart() ); + $this->assertEquals( '33.69', number_format( $cart_item['line_total'] + $cart_item['line_tax'], 2, '.', '' ) ); + + // Clean up. + WC()->cart->empty_cart(); + WC()->cart->remove_coupons(); + WC_Helper_Product::delete_product( $product->get_id() ); + WC_Helper_Coupon::delete_coupon( $coupon->get_id() ); + $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rates" ); + $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rate_locations" ); + update_option( 'woocommerce_prices_include_tax', 'no' ); + update_option( 'woocommerce_calc_taxes', 'no' ); + } + /** * Test get_remove_url. * diff --git a/tests/unit-tests/totals/totals.php b/tests/unit-tests/totals/totals.php index fde95f81510..ab43f98d7f6 100644 --- a/tests/unit-tests/totals/totals.php +++ b/tests/unit-tests/totals/totals.php @@ -147,10 +147,10 @@ class WC_Tests_Totals extends WC_Unit_Test_Case { $this->assertEquals( 40.00, $cart->fee_total ); $this->assertEquals( 27.00, $cart->cart_contents_total ); $this->assertEquals( 90.40, $cart->total ); - $this->assertEquals( 32.40, $cart->subtotal ); - $this->assertEquals( 27.00, $cart->subtotal_ex_tax ); + $this->assertEquals( 36.00, $cart->subtotal ); + $this->assertEquals( 30.00, $cart->subtotal_ex_tax ); $this->assertEquals( 11.40, $cart->tax_total ); - $this->assertEquals( 3.00, $cart->discount_cart ); + $this->assertEquals( 2.40, $cart->discount_cart ); $this->assertEquals( 0.60, $cart->discount_cart_tax ); $this->assertEquals( 40.00, $cart->fee_total ); $this->assertEquals( 10, $cart->shipping_total );