From de1bf2a5eacb308d8e3cd412ce9440a08dd68f2c Mon Sep 17 00:00:00 2001 From: sartoric <> Date: Thu, 9 Jan 2020 19:15:48 +0100 Subject: [PATCH 1/2] fix issue with the priceTax value that is not dynamically retrieved --- src/CartItem.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 5efb546..6ba3f5f 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -279,7 +279,6 @@ class CartItem implements Arrayable, Jsonable $this->id = $item->getBuyableIdentifier($this->options); $this->name = $item->getBuyableDescription($this->options); $this->price = $item->getBuyablePrice($this->options); - $this->priceTax = $this->price + $this->tax; } /** @@ -296,7 +295,6 @@ class CartItem implements Arrayable, Jsonable $this->name = Arr::get($attributes, 'name', $this->name); $this->price = Arr::get($attributes, 'price', $this->price); $this->weight = Arr::get($attributes, 'weight', $this->weight); - $this->priceTax = $this->price + $this->tax; $this->options = new CartItemOptions(Arr::get($attributes, 'options', $this->options)); $this->rowId = $this->generateRowId($this->id, $this->options->all()); From 1ffd1a5dfa8302cfb6cd55040b019d0e0b9adc4f Mon Sep 17 00:00:00 2001 From: sartoric <> Date: Fri, 10 Jan 2020 01:54:45 +0100 Subject: [PATCH 2/2] test added --- tests/CartTest.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/CartTest.php b/tests/CartTest.php index 5b28cae..e48d5b6 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -959,6 +959,52 @@ class CartTest extends TestCase $this->assertEquals(11.90, $cartItem->total(2)); } + /** @test */ + public function it_can_calculate_all_values_after_updating_from_array() + { + $cart = $this->getCartDiscount(50); + $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); + + $cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty'=>2]); + + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + + $this->assertEquals(10.00, $cartItem->price(2)); + $this->assertEquals(5.00, $cartItem->discount(2)); + $this->assertEquals(10.00, $cartItem->discountTotal(2)); + $this->assertEquals(5.00, $cartItem->priceTarget(2)); + $this->assertEquals(10.00, $cartItem->subtotal(2)); + $this->assertEquals(0.95, $cartItem->tax(2)); + $this->assertEquals(1.90, $cartItem->taxTotal(2)); + $this->assertEquals(5.95, $cartItem->priceTax(2)); + $this->assertEquals(11.90, $cartItem->total(2)); + } + + /** @test */ + public function it_can_calculate_all_values_after_updating_from_buyable() + { + $cart = $this->getCartDiscount(50); + $cart->add(new BuyableProduct(1, 'First item', 5.00), 2); + + $cart->update('027c91341fd5cf4d2579b49c4b6a90da', new BuyableProduct(1, 'First item', 10.00)); + + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + + $this->assertEquals(10.00, $cartItem->price(2)); + $this->assertEquals(5.00, $cartItem->discount(2)); + $this->assertEquals(10.00, $cartItem->discountTotal(2)); + $this->assertEquals(5.00, $cartItem->priceTarget(2)); + $this->assertEquals(10.00, $cartItem->subtotal(2)); + $this->assertEquals(0.95, $cartItem->tax(2)); + $this->assertEquals(1.90, $cartItem->taxTotal(2)); + $this->assertEquals(5.95, $cartItem->priceTax(2)); + $this->assertEquals(11.90, $cartItem->total(2)); + } + /** @test */ public function it_will_destroy_the_cart_when_the_user_logs_out_and_the_config_setting_was_set_to_true() {