bigcommerce / bigcommerce-for-wordpress

A headless commerce integration for WordPress, powered by BigCommerce
https://www.bigcommerce.com/wordpress/
GNU General Public License v2.0
109 stars 49 forks source link

Negative tax value on cart page when we have discounted product in the cart. #384

Closed AlexKonoplyanko closed 1 year ago

AlexKonoplyanko commented 2 years ago

Expected behavior

Taxes should not show negative value

Actual behavior

Taxes showed as negative value

Steps to reproduce behavior

  1. Add product to cart which have a discount for customer groups created in promotions.
  2. Go to cart there will be negative value for tax

Screenshot/Video (if applicable)

image

image

Workaround or possible solution

On Cart_Mapper.php we have method calculate_total_tax, where tax calculation is happening.

private function calculate_total_tax( $cart_amount, $discount_amount, $coupons_discount_amount, $items ) {
  $item_sum = array_sum( array_map( function ( $item ) {
      return isset( $item[ 'total_list_price' ][ 'raw' ] ) ? $item[ 'total_list_price' ][ 'raw' ] : 0;
  }, $items ) );

  return $cart_amount + $discount_amount + $coupons_discount_amount - $item_sum;
}

In my case I had such values in the cart (product price 360 with 10% discount = 324) $cart_amount = 324 $discount_amount = 0 $coupon_discount_amoun = 0 $item_sum (total_list_price) = 360 Which lead to negative tax value of -36

As possible solution to resolve that issue we can to use total_sale_price, not total_list_price in calculations like this ->

private function calculate_total_tax( $cart_amount, $discount_amount, $coupons_discount_amount, $items ) {
  $item_sum = array_sum( array_map( function ( $item ) {
      return isset( $item[ 'total_sale_price' ][ 'raw' ] ) ? $item[ 'total_sale_price' ][ 'raw' ] : 0;
  }, $items ) );

  return $cart_amount + $discount_amount + $coupons_discount_amount - $item_sum;
}
MlKilderkin commented 1 year ago

The issue is addressed in 4.36