Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravelshoppingcart

> 后端框架
Open source

Shopping Cart Implementation for Laravel Framework

1.4K stars0 likes1 views
WebsiteGitHub

About

Shopping Cart Implementation for Laravel Framework

Laravel 5 & 6 , 7 & 9 Shopping Cart

A Shopping Cart Implementation for Laravel Framework

QUICK PARTIAL DEMO

Demo: https://shoppingcart-demo.darrylfernandez.com/cart

Git repo of the demo: https://github.com/darryldecode/laravelshoppingcart-demo

INSTALLATION

Install the package through Composer.

For Laravel 5.1~: composer require "darryldecode/cart:~2.0"

For Laravel 5.5, 5.6, or 5.7~, 9:

composer require "darryldecode/cart:~4.0" or composer require "darryldecode/cart"

CONFIGURATION

  1. Open config/app.php and add this line to your Service Providers Array.
php
Darryldecode\Cart\CartServiceProvider::class
  1. Open config/app.php and add this line to your Aliases
php
  'Cart' => Darryldecode\Cart\Facades\CartFacade::class
  1. Optional configuration file (useful if you plan to have full control)
php
php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --tag="config"

HOW TO USE

  • Quick Usage
  • Usage
  • Conditions
  • Items
  • Associating Models
  • Instances
  • Exceptions
  • Events
  • Format Response
  • Examples
  • Using Different Storage
  • License

Quick Usage Example

…

Usage

IMPORTANT NOTE!

By default, the cart has a default sessionKey that holds the cart data. This also serves as a cart unique identifier which you can use to bind a cart to a specific user. To override this default session Key, you will just simply call the \Cart::session($sessionKey) method BEFORE ANY OTHER METHODS!!.

Example:

php
$userId // the current login user id

// This tells the cart that we only need or manipulate
// the cart data of a specific user. It doesn't need to be $userId,
// you can use any unique key that represents a unique to a user or customer.
// basically this binds the cart to a specific user.
\Cart::session($userId);

// then followed by the normal cart usage
\Cart::add();
\Cart::update();
\Cart::remove();
\Cart::condition($condition1);
\Cart::getTotal();
\Cart::getSubTotal();
\Cart::getSubTotalWithoutConditions();
\Cart::addItemCondition($productID, $coupon101);
// and so on..

See More Examples below:

Adding Item on Cart: Cart::add()

There are several ways you can add items on your cart, see below:

…

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

Updating an item on a cart is very simple:

…

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

Removing an item on a cart is very easy:

php
/**
 * removes an item on cart by item ID
 *
 * @param $id
 */

Cart::remove(456);

// removing cart item for a specific user's cart
$userId = auth()->user()->id; // or any string represents user identifier
Cart::session($userId)->remove(456);

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

php

/**
 * get an item on a cart by item ID
 * if item ID is not found, this will return null
 *
 * @param $itemId
 * @return null|array
 */

$itemId = 456;

Cart::get($itemId);

// You can also get the sum of the Item multiplied by its quantity, see below:
$summedPrice = Cart::get($itemId)->getPriceSum();

// get an item on a cart by item ID for a specific user's cart
$userId = auth()->user()->id; // or any string represents user identifier
Cart::session($userId)->get($itemId);

Getting cart's contents and count: Cart::getContent()

php

/**
 * get the cart
 *
 * @return CartCollection
 */

$cartCollection = Cart::getContent();

// NOTE: Because cart collection extends Laravel's Collection
// You can use methods you already know about Laravel's Collection
// See some of its method below:

// count carts contents
$cartCollection->count();

// transformations
$cartCollection->toArray();
$cartCollection->toJson();

// Getting cart's contents for a specific user
$userId = auth()->user()->id; // or any string represents user identifier
Cart::session($userId)->getContent($itemId);

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

php
/**
* check if cart is empty
*
* @return bool
*/
Cart::isEmpty();

// Check if cart's contents is empty for a specific user
$userId = auth()->user()->id; // or any string represents user identifier
Cart::session($userId)->isEmpty();

Get cart total quantity: Cart::getTotalQuantity()

php
/**
* get total quantity of items in the cart
*
* @return int
*/
$cartTotalQuantity = Cart::getTotalQuantity();

// for a specific user
$cartTotalQuantity = Cart::session($userId)->getTotalQuantity();

Get cart subtotal: Cart::getSubTotal()

php
/**
* get cart sub total
*
* @return float
*/
$subTotal = Cart::getSubTotal();

// for a specific user
$subTotal = Cart::session($userId)->getSubTotal();

Get cart subtotal with out conditions: Cart::getSubTotalWithoutConditions()

php
/**
* get cart sub total with out conditions
*
* @param bool $formatted
* @return float
*/
$subTotalWithoutConditions = Cart::getSubTotalWithoutConditions();

// for a specific user
$subTotalWithoutConditions = Cart::session($userId)->getSubTotalWithoutConditions();

Get cart total: Cart::getTotal()

php
/**
 * the new total in which conditions are already applied
 *
 * @return float
 */
$total = Cart::getTotal();

// for a specific user
$total = Cart::session($userId)->getTotal();

Clearing the Cart: Cart::clear()

php
/**
* clear cart
*
* @return void
*/
Cart::clear();
Cart::session($userId)->clear();

Conditions

Laravel Shopping Cart supports cart conditions. Conditions are very useful in terms of (coupons,discounts,sale,per-item sale and discounts etc.) See below carefully on how to use conditions.

Conditions can be added on:

1.) Whole Cart Value bases

2.) Per-Item Bases

First let's add a condition on a Cart Bases:

There are also several ways of adding a condition on a cart: NOTE:

When adding a condition on a cart bases, the 'target' should have value of 'subtotal' or 'total'. If the target is "subtotal" then this condition will be applied to subtotal. If the target is "total" then this condition will be applied to total. The order of operation also during calculation will vary on the order you have added the conditions.

Also, when adding conditions, the 'value' field will be the bases of calculation. You can change this order by adding 'order' parameter in CartCondition.

…

NOTE: All cart based conditions should be added to cart's conditions before calling Cart::getTotal() and if there are also conditions that are targeted to be applied to subtotal, it should be added to cart's conditions before calling Cart::getSubTotal()

php
$cartTotal = Cart::getSubTotal(); // the subtotal with the conditions targeted to "subtotal" applied
$cartTotal = Cart::getTotal(); // the total with the conditions targeted to "total" applied
$cartTotal = Cart::session($userId)->getSubTotal(); // for a specific user's cart
$cartTotal = Cart::session($userId)->getTotal(); // for a specific user's cart

Next is the Condition on Per-Item Bases.

This is very useful if you have coupons to be applied specifically on an item and not on the whole cart value.

NOTE: When adding a condition on a per-item bases, the 'target' parameter is not needed or can be omitted. unlike when adding conditions or per cart bases.

Now let's add condition on an item.

…

NOTE: All cart per-item conditions should be added before calling Cart::getSubTotal()

Then Finally you can call Cart::getSubTotal() to get the Cart sub total with the applied conditions on each of the items.

php
// the subtotal will be calculated based on the conditions added that has target => "subtotal"
// and also conditions that are added on per item
$cartSubTotal = Cart::getSubTotal();

Add condition to existing Item on the cart: Cart::addItemCondition($productId, $itemCondition)

Adding Condition to an existing Item on the cart is simple as well.

This is very useful when adding new conditions on an item during checkout process like coupons and promo codes. Let's see the example how to do it:

php
$productID = 456;
$coupon101 = new CartCondition(array(
            'name' => 'COUPON 101',
            'type' => 'coupon',
            'value' => '-5%',
        ));

Cart::addItemCondition($productID, $coupon101);

Clearing Cart Conditions: Cart::clearCartConditions()

php
/**
* clears all conditions on a cart,
* this does not remove conditions that has been added specifically to an item/product.
* If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId,$conditionName)
*
* @return void
*/
Cart::clearCartConditions()

Remove Specific Cart Condition: Cart::removeCartCondition($conditionName)

php
/**
* removes a condition on a cart by condition name,
* this can only remove conditions that are added on cart bases not conditions that are added on an item/product.
* If you wish to remove a condition that has been added for a specific item/product, you may
* use the removeItemCondition(itemId, conditionName) method instead.
*
* @param $conditionName
* @return void
*/
$conditionName = 'Summer Sale 5%';

Cart::removeCartCondition($conditionName)

Remove Specific Item Condition: Cart::removeItemCondition($itemId, $conditionName)

php
/**
* remove a condition that has been applied on an item that is already on the cart
*
* @param $itemId
* @param $conditionName
* @return bool
*/
Cart::removeItemCondition($itemId, $conditionName)

Clear all Item Conditions: Cart::clearItemConditions($itemId)

php
/**
* remove all conditions that has been applied on an item that is already on the cart
*
* @param $itemId
* @return bool
*/
Cart::clearItemConditions($itemId)

Get conditions by type: Cart::getConditionsByType($type)

php
/**
* Get all the condition filtered by Type
* Please Note that this will only return condition added on cart bases, not those conditions added
* specifically on an per item bases
*
* @param $type
* @return CartConditionCollection
*/
public function getConditionsByType($type)

Remove conditions by type: Cart::removeConditionsByType($type)

php
/**
* Remove all the condition with the $type specified
* Please Note that this will only remove condition added on cart bases, not those conditions added
* specifically on an per item bases
*
* @param $type
* @return $this
*/
public function removeConditionsByType($type)

Items

The method Cart::getContent() returns a collection of items.

To get the id of an item, use the property $item->id.

To get the name of an item, use the property $item->name.

To get the quantity of an item, use the property $item->quantity.

To get the attributes of an item, use the property $item->attributes.

To get the price of a single item without the conditions applied, use the property $item->price.

To get the subtotal of an item without the conditions applied, use the method $item->getPriceSum().

php
/**
* get the sum of price
*
* @return mixed|null
*/
public function getPriceSum()

To get the price of a single item without the conditions applied, use the method

$item->getPriceWithConditions().

php
/**
* get the single price in which conditions are already applied
*
* @return mixed|null
*/
public function getPriceWithConditions()

To get the subtotal of an item with the conditions applied, use the method

$item->getPriceSumWithConditions()

php
/**
* get the sum of price in which conditions are already applied
*
* @return mixed|null
*/
public function getPriceSumWithConditions()

NOTE: When you get price with conditions applied, only the conditions assigned to the current item will be calculated. Cart conditions won't be applied to price.

Associating Models

One can associate

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPlaravellaravel-5-cartlaravel-5-shopping-cartlaravel-5-shoppingcart

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架