diff --git a/packages/core/src/Pipelines/Cart/CalculateTax.php b/packages/core/src/Pipelines/Cart/CalculateTax.php index 6927e071d2..ce14d05237 100644 --- a/packages/core/src/Pipelines/Cart/CalculateTax.php +++ b/packages/core/src/Pipelines/Cart/CalculateTax.php @@ -75,11 +75,6 @@ public function handle(Cart $cart, Closure $next) $taxTotal += $shippingTaxTotal?->value; - if ($cart->shippingAddress && ! $cart->shippingOptionOverride) { - $cart->shippingAddress->taxBreakdown = $shippingTax; - $cart->shippingAddress->shippingTaxTotal = $shippingTaxTotal; - } - $taxBreakDownAmounts = $taxBreakDownAmounts->merge( $shippingTax->amounts ); @@ -93,6 +88,16 @@ public function handle(Cart $cart, Closure $next) $cart->currency, 1 ); + + if ($cart->shippingAddress && ! $cart->shippingOptionOverride) { + $cart->shippingAddress->taxBreakdown = $shippingTax; + $cart->shippingAddress->shippingTaxTotal = $shippingTaxTotal; + $cart->shippingAddress->shippingTotal = new Price( + $shippingTotal, + $cart->currency, + 1 + ); + } } $cart->taxTotal = new Price($taxTotal, $cart->currency, 1); diff --git a/tests/core/Stubs/TestTaxDriver.php b/tests/core/Stubs/TestTaxDriver.php index 80a85109ac..807b5599bc 100644 --- a/tests/core/Stubs/TestTaxDriver.php +++ b/tests/core/Stubs/TestTaxDriver.php @@ -108,6 +108,12 @@ public function getBreakdown($subTotal): TaxBreakdown $variant = $this->purchasable ?: ProductVariant::factory()->create(); + if (prices_inc_tax()) { + // Remove tax from price + $totalTaxPercentage = $taxAmounts->sum('percentage') / 100; // E.g. 0.2 for 20% + $subTotal = round($subTotal / (1 + $totalTaxPercentage)); + } + foreach ($taxAmounts as $amount) { $result = round($subTotal * ($amount->percentage / 100)); diff --git a/tests/core/Unit/Models/CartTest.php b/tests/core/Unit/Models/CartTest.php index 8759032a9f..7d4e7bbd9a 100644 --- a/tests/core/Unit/Models/CartTest.php +++ b/tests/core/Unit/Models/CartTest.php @@ -741,12 +741,22 @@ expect($cart->shippingTotal->value)->toEqual(600); expect($cart->total->value)->toEqual(720); + expect($cart->shippingAddress->shippingSubTotal->value)->toEqual(500); + expect($cart->shippingAddress->shippingTaxTotal->value)->toEqual(100); + expect($cart->shippingAddress->shippingTotal->value)->toEqual(600); + Config::set('lunar.pricing.stored_inclusive_of_tax', true); $cart->recalculate(); + expect($cart->subTotal->value)->toEqual(100); + expect($cart->shippingSubTotal->value)->toEqual(500); expect($cart->shippingTotal->value)->toEqual(500); expect($cart->total->value)->toEqual(600); + + expect($cart->shippingAddress->shippingSubTotal->value)->toEqual(500); + expect($cart->shippingAddress->shippingTaxTotal->value)->toEqual(83); + expect($cart->shippingAddress->shippingTotal->value)->toEqual(500); }); test('can create a discount breakdown', function () {