mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
Merge pull request #35 from Sartoric/master
Added a method to erase stored cart
This commit is contained in:
@@ -545,6 +545,14 @@ If you want to merge the cart with another one from the database, all you have t
|
||||
// Merge the contents of 'savedcart' into 'username'.
|
||||
Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd);
|
||||
|
||||
### Erasing the cart
|
||||
If you want to erase the cart from the database, all you have to do is call the `erase($identifier)` where `$identifier` is the key you specified for the `store` method.
|
||||
|
||||
Cart::erase('username');
|
||||
|
||||
// To erase a cart switching to an instance named 'wishlist'
|
||||
Cart::instance('wishlist')->erase('username');
|
||||
|
||||
## Exceptions
|
||||
|
||||
The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:
|
||||
@@ -567,6 +575,7 @@ The cart also has events build in. There are five events available for you to li
|
||||
| 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
|
||||
|
||||
|
||||
22
src/Cart.php
22
src/Cart.php
@@ -638,6 +638,28 @@ class Cart
|
||||
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase the cart with the given identifier.
|
||||
*
|
||||
* @param mixed $identifier
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function erase($identifier)
|
||||
{
|
||||
if ($identifier instanceof InstanceIdentifier) {
|
||||
$identifier = $identifier->getInstanceIdentifier();
|
||||
}
|
||||
|
||||
if (!$this->storedCartWithIdentifierExists($identifier)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
|
||||
|
||||
$this->events->dispatch('cart.erased');
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the contents of another cart into this cart.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user