Docs / Laravel Cart / Basic Usage

Basic Usage

The cart has a default sessionKey that holds the cart data and stores it in the session, so you can have multiple carts for multiple users.

This also serves as a cart unique identifier which you can use to bind a cart to a specific user if you want to.

Make sure to call \Cart::setSessionKey($sessionKey) before calling any other cart methods.

Usually this is not required.

// Binds the cart to a unique id (user id, session id, etc.) \Cart::setSessionKey(User::first()->id);

Adding to the cart: Cart::add()

There are a few ways to add items to the cart.

// Add a simple product to the cart Cart::add( 455, # product id 'Sample Item', # product name 100.99, # product price 2, # quantity [] # optional attributes ); // array format Cart::add([ 456, # product id 'Leather Shoes', # product name 187, # product price 1, # quantity [] # optional attributes ]); // add an item with attributes Cart::add([ 457, // product id 'T-Shirt', // product name 29.99, // product price 1, // quantity [ 'size' => 'L', 'color' => 'Blue' ] // attributes ]); // add an item with conditions Cart::add([ 458, // product id 'Headphones', // product name 199.99, // product price 1, // quantity [], // attributes [ [ 'name' => '10% Off', 'type' => 'discount', 'value' => '-10%' ] ] // conditions ]); // add multiple items at one time Cart::add( [ 456, # product id 'Leather Shoes', # product name 187, # product price 1, # quantity [] # optional attributes ], [ 431, # product id 'Leather Jacket', # product name 254.50, # product price 1, # quantity [] # optional attributes ] );

Updating an item on a cart: Cart::update()

Cart::update( 456, # product id [ 'name' => 'New Item Name', // new item name 'price' => 98.67, // new item price as a float or string ] ); // updating a product's quantity Cart::update( 456, # product id [ 'quantity' => 2, // by default adding the quantity (so if from 4 to 6) ] ); // reducing it... Cart::update( 456, [ 'quantity' => -1, // so if from 4 to 3 ] ); // you can replace the quantity by setting relative to false Cart::update( 456, # product id [ 'quantity' => [ 'relative' => false, 'value' => 5 // if the quantity was 2, it will now be 5 ], ] );

Removing an item on a cart: Cart::remove()

// Remove an item from the cart by its id Cart::remove(456);

Getting an item on a cart: Cart::get()

// Get an item from the cart by its id Cart::get(456); // You can also get the total price of the item $summedPrice = Cart::get($itemId)->getPriceSum();

Getting the cart content: Cart::getContent()

// Returns a collection of the cart's contents $cartData = Cart::getContent(); // Gets the total number of items (not quantity) in the cart $cartCollection->count(); // Transform the collection to an array or a JSON $cartCollection->toArray(); $cartCollection->toJson();

Get cart total quantity: Cart::getTotalQuantity()

$cartTotalQuantity = Cart::getTotalQuantity();

Cart subtotal: Cart::getSubTotal()

$subTotal = Cart::getSubTotal();

Cart subtotal without conditions: Cart::getSubTotalWithoutConditions()

$subTotalWithoutConditions = Cart::getSubTotalWithoutConditions();

Cart Total: Cart::getTotal()

$total = Cart::getTotal();

Check if cart is empty: Cart::isEmpty()

Cart::isEmpty();

Clearing the Cart: Cart::clear()

This clears all items and conditions from the cart.

Cart::clear();

Clearing the cart items only: Cart::clearItems()

This clears all items, but keeps the conditions (useful for when you want to keep the conditions but remove the items)

Cart::clearItems();
Discovery Call

One call.
We'll both know.

An hour to walk through your project, ask the hard questions, and work out honestly if we're the right team for it.

  • One call per day - it gets our full attention
  • Australia's only Laravel Premier Partner
  • Senior engineers only - no juniors on your project
  • Brisbane-based, onshore team

Press Esc to close  ·  B to reopen