mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-17 13:14:58 +00:00
Removed addBatch method, the add method should be used. Also added events to the cart
This commit is contained in:
12
README.md
12
README.md
@@ -32,6 +32,7 @@ Look at one of the following topics to learn more about LaravelShoppingcart
|
||||
* [Collections](#collections)
|
||||
* [Instances](#instances)
|
||||
* [Exceptions](#exceptions)
|
||||
* [Events](#events)
|
||||
* [Example](#example)
|
||||
|
||||
## Usage
|
||||
@@ -241,6 +242,17 @@ The Cart package will throw exceptions if something goes wrong. This way it's ea
|
||||
| *ShoppingcartInvalidQtyException* | When a not numeric quantity is passed |
|
||||
| *ShoppingcartInvalidRowIDException* | When the rowId that got passed doesn't exists in the current cart |
|
||||
|
||||
## Events
|
||||
|
||||
The cart also has events build in. There are five events available for you to listen for.
|
||||
|
||||
| Event | Fired |
|
||||
|----------------------------------------------------------------|
|
||||
| cart.add($item) | When a single item is added |
|
||||
| cart.batch($items) | When a batch if items is added |
|
||||
| cart.update($rowId) | When an item in the cart is updated |
|
||||
| cart.remove($rowId) | When an item is removed from the cart |
|
||||
| cart.destroy() | When the cart is destroyed |
|
||||
|
||||
## Example
|
||||
|
||||
|
||||
@@ -11,6 +11,13 @@ class Cart {
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* Event class instance
|
||||
*
|
||||
* @var Event
|
||||
*/
|
||||
protected $event;
|
||||
|
||||
/**
|
||||
* Current cart instance
|
||||
*
|
||||
@@ -22,10 +29,12 @@ class Cart {
|
||||
* Constructor
|
||||
*
|
||||
* @param Session $session Session class instance
|
||||
* @param Event $event Event class instance
|
||||
*/
|
||||
public function __construct($session)
|
||||
public function __construct($session, $event)
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->event = $event;
|
||||
|
||||
$this->instance = 'main';
|
||||
}
|
||||
@@ -64,6 +73,9 @@ class Cart {
|
||||
// recursively call the add function
|
||||
if($this->is_multi($id))
|
||||
{
|
||||
// Fire the cart.batch event
|
||||
$this->event->fire('cart.batch', $id);
|
||||
|
||||
foreach($id as $item)
|
||||
{
|
||||
$options = isset($item['options']) ? $item['options'] : array();
|
||||
@@ -74,22 +86,17 @@ class Cart {
|
||||
}
|
||||
|
||||
$options = isset($id['options']) ? $id['options'] : array();
|
||||
|
||||
// Fire the cart.add event
|
||||
$this->event->fire('cart.add', array_merge($id, array('options' => $options)));
|
||||
|
||||
return $this->addRow($id['id'], $id['name'], $id['qty'], $id['price'], $options);
|
||||
}
|
||||
|
||||
return $this->addRow($id, $name, $qty, $price, $options);
|
||||
}
|
||||
// Fire the cart.add event
|
||||
$this->event->fire('cart.add', compact('id', 'name', 'qty', 'price', 'options'));
|
||||
|
||||
/**
|
||||
* Add multiple rows to the cart
|
||||
* Maps to add() function
|
||||
* Will probably be removed in future versions
|
||||
*
|
||||
* @param Array $items An array of items to add, use array keys corresponding to the 'add' method's parameters
|
||||
*/
|
||||
public function addBatch(Array $items)
|
||||
{
|
||||
return $this->add($items);
|
||||
return $this->addRow($id, $name, $qty, $price, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,9 +112,15 @@ class Cart {
|
||||
|
||||
if(is_array($attribute))
|
||||
{
|
||||
// Fire the cart.update event
|
||||
$this->event->fire('cart.update', $rowId);
|
||||
|
||||
return $this->updateAttribute($rowId, $attribute);
|
||||
}
|
||||
|
||||
// Fire the cart.update event
|
||||
$this->event->fire('cart.update', $rowId);
|
||||
|
||||
return $this->updateQty($rowId, $attribute);
|
||||
}
|
||||
|
||||
@@ -123,6 +136,9 @@ class Cart {
|
||||
|
||||
$cart = $this->getContent();
|
||||
|
||||
// Fire the cart.remove event
|
||||
$this->event->fire('cart.remove', $rowId);
|
||||
|
||||
$cart->forget($rowId);
|
||||
|
||||
return $this->updateCart($cart);
|
||||
@@ -160,6 +176,9 @@ class Cart {
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
// Fire the cart.destroy event
|
||||
$this->event->fire('cart.destroy');
|
||||
|
||||
return $this->updateCart(NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ class ShoppingcartServiceProvider extends ServiceProvider {
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
|
||||
$this->app['cart'] = $this->app->share(function($app)
|
||||
{
|
||||
$session = $app['session'];
|
||||
return new Cart($session);
|
||||
$event = $app['events'];
|
||||
return new Cart($session, $event);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user