$ composer require juliobitencourt/laravel-cart
{
"require": {
"juliobitencourt/laravel-cart": "1.0.*"
}
}
Add the service provider to your app/config/app.php in the service providers array
'JulioBitencourt\Cart\CartServiceProvider',
Publish the resources
php artisan vendor:publish
Check the config/laravel-cart.php file. The storage-driver config can have the values Session (default) or Database.
In case you use Database as the storage driver you have to run the migrations
php artisan migrate
use JulioBitencourt\Cart\Cart;
class CartController extends Controller {
protected $cart;
public function __construct(Cart $cart)
{
$this->cart = $cart;
}
If you insert an item with the same SKU twice, the item quantity will be updated.
$item = [
'sku' => '123456',
'description' => 'PlayStation 4',
'price' => 300,
'quantity' => 1
];
$result = $this->cart->insert($item);
$item = [
'sku' => '111111',
'description' => '2 Year Protection',
'price' => 30.50,
'quantity' => 1
];
$result = $this->cart->insertChild($parentId, $item);
If you update an item with 0 quantity, it will be removed from the cart's list of items
$result = $this->cart->update($id, $quantity);
$this->cart->delete($id);
$this->cart->destroy();
$this->cart->isEmpty(); // Returns true or false
$this->cart->all();
$this->cart->totalItems();
$this->cart->total();
This is useful if you want to implement abandoned cart recovery.
$this->cart->setEmail($email)