A Simple Session Based Database Driven Shopping Cart Library For Laravel.
It's Really Simple & Easy To Manage Your Shopping Cart Item At Laravel
Download The Class Library and put it to app/Http/Libraries folder
Now Load the Cart Class To your base controller
use App\Http\Libraries\Cart;
Now you're ready to start using the shoppingcart in your application.
Add the table to your database,
database/order_data_temps.sql
The shopping cart gives you the following methods to use:
You Need to Create an Object Of the Cart Class Then
$cart = new \App\Http\Libraries\Cart();
If you want to set your rename table instead of default then just pass the name to setTable()
method
$cart->setTable('my_custom_order_table');
Note: The Table must have the columns that are exist in default table.
Adding an item to the cart is really simple, you just use the insert()
method, which accepts a variety of parameters.
In its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart.
$data = array(
'product_id' => 4,
'quantity' => 5,
'price' => 258,
'name' => 'child dress'
);
$cart->insert($data);
As an optional fifth parameter you can pass others items information as options, so you can add multiple items with the same id, but with a different size or color etc.
$data = array(
'product_id' => 4,
'quantity' => 5,
'price' => 258,
'name' => 'child dress',
'options' => ['size'=> 'L', 'type' => 'Silk', 'Manufacturer' => 'Cat\'s Eye']
);
$cart->insert($data);
To update an item in the cart, you'll first need the rowId of the item.
Next you can use the update()
method to update it.
If you want to update any item then pass it to array and it will replace, you'll pass the update method the rowId and the new info of product:
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
$data = array(
'quantity' => 5,
'name' => 'child dress',
'options' => ['size'=> 'L', 'type' => 'Silk', 'Manufacturer' => 'Cat\'s Eye']
);
$cart->update($rowId, $data);
YYou just class the getItems()
method to get all cart items
$cart->getItems();
To remove an item from the cart, you'll again need the rowId. This rowId you simply pass to the removeItem()
method and it will remove the item from the cart.
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
$cart->removeItem($rowId);
If you want to completely remove the content of a cart, you can call the removeAllItems method on the cart. This will remove all CartItems from the cart.
$cart->removeAllItems();
The total()
method can be used to get the calculated total of all items in the cart, based on there price and quantity.
$cart->total();
If you want to know how many items there are in your cart, you can use the total_items()
method. This method will return the total number of items in the cart. So if you've added 2 Pens and 1 umbrella, it will return 3 items.
$cart->total_items()