mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-23 15:41:24 +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)
|
* [Collections](#collections)
|
||||||
* [Instances](#instances)
|
* [Instances](#instances)
|
||||||
* [Exceptions](#exceptions)
|
* [Exceptions](#exceptions)
|
||||||
|
* [Events](#events)
|
||||||
* [Example](#example)
|
* [Example](#example)
|
||||||
|
|
||||||
## Usage
|
## 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 |
|
| *ShoppingcartInvalidQtyException* | When a not numeric quantity is passed |
|
||||||
| *ShoppingcartInvalidRowIDException* | When the rowId that got passed doesn't exists in the current cart |
|
| *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
|
## Example
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,13 @@ class Cart {
|
|||||||
*/
|
*/
|
||||||
protected $session;
|
protected $session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event class instance
|
||||||
|
*
|
||||||
|
* @var Event
|
||||||
|
*/
|
||||||
|
protected $event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current cart instance
|
* Current cart instance
|
||||||
*
|
*
|
||||||
@@ -22,10 +29,12 @@ class Cart {
|
|||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param Session $session Session class instance
|
* @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->session = $session;
|
||||||
|
$this->event = $event;
|
||||||
|
|
||||||
$this->instance = 'main';
|
$this->instance = 'main';
|
||||||
}
|
}
|
||||||
@@ -64,6 +73,9 @@ class Cart {
|
|||||||
// recursively call the add function
|
// recursively call the add function
|
||||||
if($this->is_multi($id))
|
if($this->is_multi($id))
|
||||||
{
|
{
|
||||||
|
// Fire the cart.batch event
|
||||||
|
$this->event->fire('cart.batch', $id);
|
||||||
|
|
||||||
foreach($id as $item)
|
foreach($id as $item)
|
||||||
{
|
{
|
||||||
$options = isset($item['options']) ? $item['options'] : array();
|
$options = isset($item['options']) ? $item['options'] : array();
|
||||||
@@ -74,22 +86,17 @@ class Cart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$options = isset($id['options']) ? $id['options'] : array();
|
$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['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'));
|
||||||
|
|
||||||
/**
|
return $this->addRow($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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,9 +112,15 @@ class Cart {
|
|||||||
|
|
||||||
if(is_array($attribute))
|
if(is_array($attribute))
|
||||||
{
|
{
|
||||||
|
// Fire the cart.update event
|
||||||
|
$this->event->fire('cart.update', $rowId);
|
||||||
|
|
||||||
return $this->updateAttribute($rowId, $attribute);
|
return $this->updateAttribute($rowId, $attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fire the cart.update event
|
||||||
|
$this->event->fire('cart.update', $rowId);
|
||||||
|
|
||||||
return $this->updateQty($rowId, $attribute);
|
return $this->updateQty($rowId, $attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,6 +136,9 @@ class Cart {
|
|||||||
|
|
||||||
$cart = $this->getContent();
|
$cart = $this->getContent();
|
||||||
|
|
||||||
|
// Fire the cart.remove event
|
||||||
|
$this->event->fire('cart.remove', $rowId);
|
||||||
|
|
||||||
$cart->forget($rowId);
|
$cart->forget($rowId);
|
||||||
|
|
||||||
return $this->updateCart($cart);
|
return $this->updateCart($cart);
|
||||||
@@ -160,6 +176,9 @@ class Cart {
|
|||||||
*/
|
*/
|
||||||
public function destroy()
|
public function destroy()
|
||||||
{
|
{
|
||||||
|
// Fire the cart.destroy event
|
||||||
|
$this->event->fire('cart.destroy');
|
||||||
|
|
||||||
return $this->updateCart(NULL);
|
return $this->updateCart(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,11 +11,11 @@ class ShoppingcartServiceProvider extends ServiceProvider {
|
|||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
|
|
||||||
$this->app['cart'] = $this->app->share(function($app)
|
$this->app['cart'] = $this->app->share(function($app)
|
||||||
{
|
{
|
||||||
$session = $app['session'];
|
$session = $app['session'];
|
||||||
return new Cart($session);
|
$event = $app['events'];
|
||||||
|
return new Cart($session, $event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user