Skip to content

Commit

Permalink
Merge pull request #104 from manojo123/master
Browse files Browse the repository at this point in the history
Add new events (adding, updating, removing)
  • Loading branch information
bumbummen99 authored Sep 23, 2020
2 parents 2d11e6a + 0991d4e commit b05497d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,18 @@ The Cart package will throw exceptions if something goes wrong. This way it's ea

The cart also has events build in. There are five events available for you to listen for.

| Event | Fired | Parameter |
| ------------- | ---------------------------------------- | -------------------------------- |
| 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. | - |
| cart.erased | When the content of a cart was erased. | - |
| Event | Fired | Parameter |
| ------------- | ---------------------------------------- | ------------------------------------- |
| cart.adding | When adding an item to the cart. | The `CartItem` that is being added. |
| cart.updating | When updating an item to the cart. | The `CartItem` that is being updated. |
| cart.removing | When removing an item to the cart. | The `CartItem` that is being removed. |
| cart.added | When an item was added to the cart. | The `CartItem` that was added. |
| cart.updated | When an item was updated to the cart. | The `CartItem` that was updated. |
| cart.removed | When an item was 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. | - |
| cart.erased | When the content of a cart was erased. | - |

## Example

Expand Down
14 changes: 11 additions & 3 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ public function addCartItem($item, $keepDiscount = false, $keepTax = false, $dis
$content->put($item->rowId, $item);

if ($dispatchEvent) {
$this->events->dispatch('cart.added', $item);
$this->events->dispatch('cart.adding', $item);
}

$this->session->put($this->instance, $content);

if ($dispatchEvent) {
$this->events->dispatch('cart.added', $item);
}

return $item;
}

Expand Down Expand Up @@ -222,10 +226,12 @@ public function update($rowId, $qty)
}
}

$this->events->dispatch('cart.updated', $cartItem);
$this->events->dispatch('cart.updating', $cartItem);

$this->session->put($this->instance, $content);

$this->events->dispatch('cart.updated', $cartItem);

return $cartItem;
}

Expand All @@ -244,9 +250,11 @@ public function remove($rowId)

$content->pull($cartItem->rowId);

$this->events->dispatch('cart.removed', $cartItem);
$this->events->dispatch('cart.removing', $cartItem);

$this->session->put($this->instance, $content);

$this->events->dispatch('cart.removed', $cartItem);
}

/**
Expand Down

0 comments on commit b05497d

Please sign in to comment.