juliobitencourt / laravel-cart
A lightweight PHP Shopping Cart for Laravel 5.
Installs: 1 300
Dependents: 0
Suggesters: 0
Security: 0
Stars: 38
Watchers: 3
Forks: 15
Open Issues: 4
Requires
- php: >=5.4.0
- illuminate/database: ~5.0|~5.1@dev
- illuminate/session: *
Requires (Dev)
- phpspec/phpspec: ~2.1
This package is not auto-updated.
Last update: 2024-01-20 13:42:13 UTC
README
A lightweight PHP Shopping Cart for Laravel 5.
Installation
With Composer
$ 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
Usage
Inject the class into the constructor.
use JulioBitencourt\Cart\Cart; class CartController extends Controller { protected $cart; public function __construct(Cart $cart) { $this->cart = $cart; }
Insert a new Item to the 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);
Insert a Child Item.
$item = [ 'sku' => '111111', 'description' => '2 Year Protection', 'price' => 30.50, 'quantity' => 1 ]; $result = $this->cart->insertChild($parentId, $item);
Update an 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);
Remove an item
$this->cart->delete($id);
Destroy the cart
$this->cart->destroy();
Checking if the cart is empty
$this->cart->isEmpty(); // Returns true or false
Returning an array with the list of items
$this->cart->all();
Count the total of items
$this->cart->totalItems();
Sum the cart total price
$this->cart->total();
Identify the cart with the e-mail.
This is useful if you want to implement abandoned cart recovery.
$this->cart->setEmail($email)