mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
Dispatch "cart.merged" event,added a parameter to avoid dispatching "cart.added" events while merging
This commit is contained in:
@@ -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. | - |
|
||||
|
||||
|
||||
17
src/Cart.php
17
src/Cart.php
@@ -46,7 +46,7 @@ class Cart
|
||||
private $discount = 0;
|
||||
|
||||
/**
|
||||
* Defines the discount percentage.
|
||||
* Defines the tax rate
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
@@ -129,10 +129,11 @@ class 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 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user