How to use Shopping Cart in Laravel?
Add Item
You can pass a simple or a multidimensional array and to help you get started, we have listed below all the default indexes that you can pass when adding or updating a cart item.
Add a single item
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
]);
Add a single item with a custom index
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'sku' => 'tshirt-custom',
]);
Add a single item with attributes and a custom index
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'sku' => 'tshirt-red-large',
'attributes' => [
'color' => [
'label' => 'Red',
'value' => 'red',
],
'size' => [
'label' => 'Large',
'value' => 'l',
],
],
]);
Adding multiple items
Cart::add([
[
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'sku' => 'tshirt-red-large',
'attributes' => [
'color' => [
'label' => 'Red',
'value' => 'red',
],
'size' => [
'label' => 'Large',
'value' => 'l',
],
],
],
[
'id' => 'sweatshirt',
'name' => 'Sweatshirt',
'quantity' => 1,
'price' => 98.32,
],
]);
Update Item
Update an item quantity
Cart::update('c14c437bc9ae7d35a7c18ee151c6acc0', ['quantity' => 2]);
Update a single item
Cart::update('c14c437bc9ae7d35a7c18ee151c6acc0', [
'quantity' => 1,
'price' => 12.50,
]);
Update multiple items
Cart::update([
'c14c437bc9ae7d35a7c18ee151c6acc0' => [
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
],
'63e2d7033fe95b9134a5737503d10ba5' => [
'id' => 'sweatshirt',
'name' => 'Sweatshirt',
'quantity' => 2,
'price' => 98.32,
],
]);
Remove Item
Remove a single item
Cart::remove('c14c437bc9ae7d35a7c18ee151c6acc0');
Remove multiple items
Cart::remove([
'c14c437bc9ae7d35a7c18ee151c6acc0',
'63e2d7033fe95b9134a5737503d10ba5',
]);
Get all the items
$items = Cart::items();
foreach ($items as $item)
{
echo $item->price();
}
Check if an item exists
if (Cart::exists('c14c437bc9ae7d35a7c18ee151c6acc0'))
{
Cart::remove('c14c437bc9ae7d35a7c18ee151c6acc0');
}
Get a single item
$item = Cart::item('c14c437bc9ae7d35a7c18ee151c6acc0');
Get the item price
Get the item price + the item attributes total price
Get the item quantity
Get the item subtotal
Get the item weight
Get the item attributes
Cart::total()
Cart::subtotal()
Cart::quantity()
Cart::weight()
Cart::itemsSubtotal()
echo Cart::itemsSubtotal();
Cart::clear()
Cart::getIdentity()
Cart::setIdentity()
Cart::setIdentity('my-new-cart-name');
Cart::sync()
This method is very useful when you want to synchronize a shopping cart that is stored on the database for example.
$items = [
[
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
],
[
'id' => 'sweatshirt',
'name' => 'Sweatshirt',
'quantity' => 1,
'price' => 98.32,
],
];
$collection = new Collection($items);
Cart::sync($collection);
Metadata
Managing metadata inside the cart like shipping or billing information is very easy.
Setting metadata is very easy, just provide an array with a key/value pair and you're done.
Cart::setMetaData() :-
$data = [
'full_name' => 'John Doe',
'address' => 'Example Street',
];
Cart::setMetaData('shipping_information', $data);
Cart::setMetaData('shipping_information.street', 'Street ABC');
Cart::getMetaData()
To return all the available metadata
$metadata = Cart::getMetaData();
To return metadata by keys
$metadata = Cart::getMetaData('shipping_information');
$metadata = Cart::getMetaData('shipping_information.full_name');
Cart::removeMetaData()
To remove all the metadata
To remove metadata by keys
Cart::removeMetaData('shipping_information.full_name');
Cart::removeMetaData('shipping_information');
Search
You can use one or multiple properties to search for items in the cart.
Find
Cart::find([
'id' => 'foobar',
]);
Cart::find([
'name' => 'Foo Bar',
'price' => 5,
]);
Cart::find([
'attributes' => [
'size' => [
'price' => 5,
],
],
]);
Attributes
Each item can have different attributes like size, color and you can even add a price to each attribute that will reflect on the final item price.
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'attributes' => [
'size' => [
'label' => 'Large',
'value' => 'l',
'price' => 5,
],
'color' => [
'label' => 'Red',
'value' => 'red',
],
],
]);
count()
The count() method counts the number of items in the collection.
$total = Cart::items()->count();
isEmpty()
The isEmpty() method can be used to check whether or not the collection has elements within it. It accepts no value and returns a boolean.
if ( ! Cart::items()->isEmpty())
{
echo 'We have items on our cart :)';
}
else
{
echo 'Cart is empty :(';
}
Previous
Next