Dispatch "cart.merged" event,added a parameter to avoid dispatching "cart.added" events while merging

This commit is contained in:
sartoric
2019-11-18 03:14:53 +01:00
committed by sartoric
parent 18c89a495b
commit 1341863d65
3 changed files with 77 additions and 12 deletions

View File

@@ -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;
}