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');
|
Cart::instance('wishlist')->restore('username');
|
||||||
|
|
||||||
### Merge the cart
|
### 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'.
|
// Merge the contents of 'savedcart' into 'username'.
|
||||||
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate);
|
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd);
|
||||||
|
|
||||||
## Exceptions
|
## 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.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.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.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.stored | When the content of a cart was stored. | - |
|
||||||
| cart.restored | When the content of a cart was restored. | - |
|
| cart.restored | When the content of a cart was restored. | - |
|
||||||
|
|
||||||
|
|||||||
25
src/Cart.php
25
src/Cart.php
@@ -46,7 +46,7 @@ class Cart
|
|||||||
private $discount = 0;
|
private $discount = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the discount percentage.
|
* Defines the tax rate
|
||||||
*
|
*
|
||||||
* @var float
|
* @var float
|
||||||
*/
|
*/
|
||||||
@@ -126,13 +126,14 @@ class Cart
|
|||||||
/**
|
/**
|
||||||
* Add an item to the cart.
|
* Add an item to the cart.
|
||||||
*
|
*
|
||||||
* @param \Gloudemans\Shoppingcart\CartItem $item Item to add 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 $keepDiscount Keep the discount rate of the Item
|
||||||
* @param bool $keepTax Keep the Tax rate of the Item
|
* @param bool $keepTax Keep the Tax rate of the Item
|
||||||
|
* @param bool $dispatchEvent
|
||||||
*
|
*
|
||||||
* @return \Gloudemans\Shoppingcart\CartItem The CartItem
|
* @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) {
|
if (!$keepDiscount) {
|
||||||
$item->setDiscountRate($this->discount);
|
$item->setDiscountRate($this->discount);
|
||||||
@@ -150,7 +151,9 @@ class Cart
|
|||||||
|
|
||||||
$content->put($item->rowId, $item);
|
$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);
|
$this->session->put($this->instance, $content);
|
||||||
|
|
||||||
@@ -632,8 +635,7 @@ class Cart
|
|||||||
|
|
||||||
$this->instance($currentInstance);
|
$this->instance($currentInstance);
|
||||||
|
|
||||||
$this->getConnection()->table($this->getTableName())
|
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
|
||||||
->where('identifier', $identifier)->delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -642,10 +644,11 @@ class Cart
|
|||||||
* @param mixed $identifier Identifier of the Cart to merge with.
|
* @param mixed $identifier Identifier of the Cart to merge with.
|
||||||
* @param bool $keepDiscount Keep the discount of the CartItems.
|
* @param bool $keepDiscount Keep the discount of the CartItems.
|
||||||
* @param bool $keepTax Keep the tax of the CartItems.
|
* @param bool $keepTax Keep the tax of the CartItems.
|
||||||
|
* @param bool $dispatchAdd Flag to dispatch the add events.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function merge($identifier, $keepDiscount = false, $keepTax = false)
|
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true)
|
||||||
{
|
{
|
||||||
if (!$this->storedCartWithIdentifierExists($identifier)) {
|
if (!$this->storedCartWithIdentifierExists($identifier)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -657,9 +660,11 @@ class Cart
|
|||||||
$storedContent = unserialize($stored->content);
|
$storedContent = unserialize($stored->content);
|
||||||
|
|
||||||
foreach ($storedContent as $cartItem) {
|
foreach ($storedContent as $cartItem) {
|
||||||
$this->addCartItem($cartItem, $keepDiscount, $keepTax);
|
$this->addCartItem($cartItem, $keepDiscount, $keepTax, $dispatchAdd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->events->dispatch('cart.merged');
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1204,6 +1204,65 @@ class CartTest extends TestCase
|
|||||||
$this->assertEquals('large', $cartItem->options->size);
|
$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.
|
* Get an instance of the cart.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user