From 1341863d65f476f4fd03f77fe88ad6781baf7e7a Mon Sep 17 00:00:00 2001 From: sartoric <> Date: Mon, 18 Nov 2019 03:14:53 +0100 Subject: [PATCH] Dispatch "cart.merged" event,added a parameter to avoid dispatching "cart.added" events while merging --- README.md | 5 ++-- src/Cart.php | 25 ++++++++++++-------- tests/CartTest.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3481422..643b5b0 100644 --- a/README.md +++ b/README.md @@ -540,10 +540,10 @@ If you want to retrieve the cart from the database and restore it, all you have Cart::instance('wishlist')->restore('username'); ### Merge the cart -If you want to merge the cart with another one from the database, all you have to do is call the `merge($identifier)` where `$identifier` is the key you specified for the `store` method. You can also define if you want to keep the discount and tax rates of the items. +If you want to merge the cart with another one from the database, all you have to do is call the `merge($identifier)` where `$identifier` is the key you specified for the `store` method. You can also define if you want to keep the discount and tax rates of the items and if you want to dispatch "cart.added" events. // Merge the contents of 'savedcart' into 'username'. - Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate); + Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd); ## Exceptions @@ -564,6 +564,7 @@ The cart also has events build in. There are five events available for you to li | cart.added | When an item was added to the cart. | The `CartItem` that was added. | | cart.updated | When an item in the cart was updated. | The `CartItem` that was updated. | | cart.removed | When an item is removed from the cart. | The `CartItem` that was removed. | +| cart.merged | When the content of a cart is merged | - | | cart.stored | When the content of a cart was stored. | - | | cart.restored | When the content of a cart was restored. | - | diff --git a/src/Cart.php b/src/Cart.php index 7f2445d..5ace253 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -46,7 +46,7 @@ class Cart private $discount = 0; /** - * Defines the discount percentage. + * Defines the tax rate * * @var float */ @@ -126,13 +126,14 @@ class Cart /** * Add an item to the cart. * - * @param \Gloudemans\Shoppingcart\CartItem $item Item to add to the Cart - * @param bool $keepDiscount Keep the discount rate of the Item - * @param bool $keepTax Keep the Tax rate of the Item + * @param \Gloudemans\Shoppingcart\CartItem $item Item to add to the Cart + * @param bool $keepDiscount Keep the discount rate of the Item + * @param bool $keepTax Keep the Tax rate of the Item + * @param bool $dispatchEvent * * @return \Gloudemans\Shoppingcart\CartItem The CartItem */ - public function addCartItem($item, $keepDiscount = false, $keepTax = false) + public function addCartItem($item, $keepDiscount = false, $keepTax = false, $dispatchEvent = true) { if (!$keepDiscount) { $item->setDiscountRate($this->discount); @@ -150,7 +151,9 @@ class Cart $content->put($item->rowId, $item); - $this->events->dispatch('cart.added', $item); + if ($dispatchEvent) { + $this->events->dispatch('cart.added', $item); + } $this->session->put($this->instance, $content); @@ -632,8 +635,7 @@ class Cart $this->instance($currentInstance); - $this->getConnection()->table($this->getTableName()) - ->where('identifier', $identifier)->delete(); + $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete(); } /** @@ -642,10 +644,11 @@ class Cart * @param mixed $identifier Identifier of the Cart to merge with. * @param bool $keepDiscount Keep the discount of the CartItems. * @param bool $keepTax Keep the tax of the CartItems. + * @param bool $dispatchAdd Flag to dispatch the add events. * * @return bool */ - public function merge($identifier, $keepDiscount = false, $keepTax = false) + public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true) { if (!$this->storedCartWithIdentifierExists($identifier)) { return false; @@ -657,9 +660,11 @@ class Cart $storedContent = unserialize($stored->content); foreach ($storedContent as $cartItem) { - $this->addCartItem($cartItem, $keepDiscount, $keepTax); + $this->addCartItem($cartItem, $keepDiscount, $keepTax, $dispatchAdd); } + $this->events->dispatch('cart.merged'); + return true; } diff --git a/tests/CartTest.php b/tests/CartTest.php index 5c7d8e5..0edf156 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1204,6 +1204,65 @@ class CartTest extends TestCase $this->assertEquals('large', $cartItem->options->size); } + /** @test */ + public function it_can_merge_without_dispatching_add_events() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + $cart = $this->getCartDiscount(50); + $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); + $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->store('test'); + + Event::fakeFor(function () { + $cart2 = $this->getCart(); + $cart2->instance('test2'); + $cart2->setGlobalTax(0); + $cart2->setGlobalDiscount(0); + + $this->assertEquals('0', $cart2->countInstances()); + + $cart2->merge('test',null,null,false); + + Event::assertNotDispatched('cart.added'); + Event::assertDispatched('cart.merged'); + + $this->assertEquals('2', $cart2->countInstances()); + $this->assertEquals(20, $cart2->totalFloat()); + }); + } + + /** @test */ + public function it_can_merge_dispatching_add_events() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + $cart = $this->getCartDiscount(50); + $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); + $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->store('test'); + + Event::fakeFor(function () { + $cart2 = $this->getCart(); + $cart2->instance('test2'); + $cart2->setGlobalTax(0); + $cart2->setGlobalDiscount(0); + + $this->assertEquals('0', $cart2->countInstances()); + + $cart2->merge('test'); + + Event::assertDispatched('cart.added',2); + Event::assertDispatched('cart.merged'); + $this->assertEquals('2', $cart2->countInstances()); + $this->assertEquals(20, $cart2->totalFloat()); + }); + } + /** * Get an instance of the cart. *