PHPackages                             wearepixel/laravel-cart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wearepixel/laravel-cart

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wearepixel/laravel-cart
=======================

A cart implementation for Laravel

3.0.0(3w ago)1374.8k↑2163.5%1[1 PRs](https://github.com/wearepixel/laravel-cart/pulls)MITPHPPHP &gt;=8.3CI passing

Since Aug 14Pushed 1w ago1 watchersCompare

[ Source](https://github.com/wearepixel/laravel-cart)[ Packagist](https://packagist.org/packages/wearepixel/laravel-cart)[ RSS](/packages/wearepixel-laravel-cart/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (44)Versions (38)Used By (0)

[![Laravel Cart](./docs/laravel-cart.png)](https://joelmale.com)[![Latest Version on Packagist](https://camo.githubusercontent.com/39ff6122eb05249ed59743d2b5951e6db2d0b4be28eb181d64a0e5586cd15bbe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7765617265706978656c2f6c61726176656c2d636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wearepixel/laravel-cart)[![GitHub Tests Action Status](https://camo.githubusercontent.com/afea70a636fbc93dadf2c36d84d1753992995ab1bff2cddc4603da5952f71c43/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7765617265706978656c2f6c61726176656c2d636172742f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d5465737473)](https://github.com/wearepixel/laravel-cart/actions?query=workflow%3ATests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/2dc292795ca87d4e0e790f9441860bb545400e212b55c278fb6d03878c2d7b4f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7765617265706978656c2f6c61726176656c2d636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wearepixel/laravel-cart)[![GitHub last commit](https://camo.githubusercontent.com/74ca521826677880c1399d70e9b643fc5af77ad103ffbe3a8c47f92c6a60c874/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f7765617265706978656c2f6c61726176656c2d63617274)](#)[![License](https://camo.githubusercontent.com/b3c90097c14c4aaef21562ba99b75adbbaeed082903caf2706eac90773596a02/68747470733a2f2f706f7365722e707567782e6f72672f7765617265706978656c2f6c61726176656c2d636172742f6c6963656e73652e737667)](https://packagist.org/packages/wearepixel/laravel-cart)[![Free](https://camo.githubusercontent.com/7f1fea4e71e3678999eb224f1e7295df2fced95120fef0b5701c652b56440c13/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f667265655f666f725f6e6f6e5f636f6d6d65726369616c5f7573652d627269676874677265656e)](#-license)

A Cart Implementation for Laravel.

Supported Laravel Versions: 10, 11, 12, and 13.

For Laravel 9.0 and below, please use version [1.0](https://github.com/wearepixel/laravel-cart/releases/tag/1.0.12)

> **Upgrading from v2?** See the [Upgrade Guide](UPGRADE.md) for breaking changes and recommended v3 patterns.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting Started](#-getting-started)
- [Demo](#-deni)
- [Documentation](#-documentation)
    - [Configuration](#configuration)
    - [Basic Usage](#basic-usage)
    - [Conditions](#conditions)
    - [Cart Items](#cart-items)
    - [Storage Drivers](#storage-drivers)
    - [Artisan Generators](#artisan-generators)
    - [First-class Objects](#first-class-objects)
    - [Testing](#testing)
    - [Livewire / Inertia](#livewire--inertia)
    - [Events](#events)
- [Credits](#-credits)
- [License](#-license)

🚀 Getting Started
-----------------

[](#-getting-started)

### 🔥 Installing

[](#-installing)

Install the package through [Composer](http://getcomposer.org/).

`composer require wearepixel/laravel-cart`

🧑‍🍳 Demo
--------

[](#‍-demo)

```
// Add an item to the cart
\Cart::add(
    1, // any unique id
    'Product 1', // product name
    19.99, // product price
    2, // quantity
    ['size' => 'large'] // an array of extra attributes
);

// get the entire cart
$cartContents = \Cart::getContent();

// update an item already in the cart
\Cart::update(
    1, // the same unique id that was used to add the item
    ['quantity' => 3] // the quantity to update
);

// remove the item by its id
\Cart::remove(1);

// get the total of the cart
$total = Cart::getTotal();

// clear it all when you've finished (like when you've stored the order)
\Cart::clear();
```

📚 Documentation
---------------

[](#-documentation)

### ⚙️ Configuration

[](#️-configuration)

You can publish the configuration file to customize various options.

```
php artisan vendor:publish --provider="Wearepixel\Cart\CartServiceProvider" --tag="cart-config"
```

#### Formatting Numbers

[](#formatting-numbers)

The package by default does not use round to format numbers, and instead returns the number using floatval().

If you'd prefer this number to be rounded, you can customize the formatting in the configuration file.

Defaults to false.

```
'format_numbers' => env('LARAVEL_CART_FORMAT_VALUES', false),
'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
```

#### Decimals

[](#decimals)

You can customize the number of decimals in the configuration file.

Defaults to 2.

```
'decimals' => env('LARAVEL_CART_DECIMALS', 2),
```

#### Round Mode

[](#round-mode)

The package uses the `round` function to round the prices. You can customize the rounding mode in the configuration file.

Defaults to `down`.

```
'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
```

### 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()**

[](#adding-to-the-cart-cartadd)

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()**

[](#updating-an-item-on-a-cart-cartupdate)

```
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()**

[](#removing-an-item-on-a-cart-cartremove)

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

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

[](#getting-an-item-on-a-cart-cartget)

```
// 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()**

[](#getting-the-cart-content-cartgetcontent)

```
// 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()**

[](#cart-subtotal-cartgetsubtotal)

```
$subTotal = Cart::getSubTotal();
```

#### Cart subtotal without conditions: **Cart::getSubTotalWithoutConditions()**

[](#cart-subtotal-without-conditions-cartgetsubtotalwithoutconditions)

```
$subTotalWithoutConditions = Cart::getSubTotalWithoutConditions();
```

Cart Total: **Cart::getTotal()**

```
$total = Cart::getTotal();
```

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

[](#check-if-cart-is-empty-cartisempty)

```
Cart::isEmpty();
```

#### Clearing the Cart: **Cart::clear()**

[](#clearing-the-cart-cartclear)

This clears all items and conditions from the cart.

```
Cart::clear();
```

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

[](#clearing-the-cart-items-only-cartclearitems)

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

```
Cart::clearItems();
```

### Conditions

[](#conditions)

Conditions are very useful for adding things like discounts, taxes, shipping, etc.

Conditions can be added on the entire cart or on individual items, and can even be applied only at certain cart values.

Conditions on a cart level should always have a `target` of `subtotal` or `total`. This tells the cart which value to apply the condition to.

You can provide an optional `minimum` value which should be the dollar value in which the target (subtotal or total) needs to be for the condition to be active and impact the cart.

You can also provide an `order` to cart conditions which tells the cart in what order to apply the conditions. Item level conditions do not support the `order` parameter.

#### Conditions on the cart

[](#conditions-on-the-cart)

##### Adding a condition to the cart: **Cart::condition()**

[](#adding-a-condition-to-the-cart-cartcondition)

```
// Add a single condition to the cart
$condition = new \Wearepixel\Cart\CartCondition([
    'name' => 'Tax: 10%',
    'type' => 'tax',
    'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
    'value' => '10%',
    'attributes' => [ // add extra attributes here
    	'description' => 'Compulsory tax',
    ]
]);

Cart::condition($condition);

// Add multiple conditions
$tax = new \Wearepixel\Cart\CartCondition([
    'name' => 'Tax: 10%',
    'type' => 'tax',
    'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
    'value' => '10%',
    'order' => 2
]);

$shipping = new \Wearepixel\Cart\CartCondition([
    'name' => 'Shipping: $15',
    'type' => 'shipping',
    'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
    'value' => '+15',
    'order' => 1
]);

Cart::condition($tax);
Cart::condition($shipping);

// or as an array
Cart::condition([$tax, $shipping]);

// add condition to only apply on totals, not in subtotal
$shipping = new \Wearepixel\Cart\CartCondition([
    'name' => 'Express Shipping $15',
    'type' => 'shipping',
    'target' => 'total',
    'value' => '+15',
    'order' => 1
]);

Cart::condition($shipping);
```

##### Getting conditions on the cart: **Cart::getConditions()**

[](#getting-conditions-on-the-cart-cartgetconditions)

```
// To get all applied conditions on a cart, use below:
$cartConditions = Cart::getConditions();

foreach($cartConditions as $condition)
{
    $condition->getTarget(); // the target of which the condition was applied
    $condition->getName(); // the name of the condition
    $condition->getType(); // the type
    $condition->getValue(); // the value of the condition
    $condition->getOrder(); // the order of the condition
    $condition->getMinimum(); // the minimum dollar amount of the target, needed to activate the condition
    $condition->getMaximum(); // the maximum dollar amount of the target, needed to keep the condition active
    $condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
}
```

##### Getting conditions on the cart as an array: **Cart::getConditions(array: true)**

[](#getting-conditions-on-the-cart-as-an-array-cartgetconditionsarray-true)

You can get all cart conditions in array format by passing "array: true". This is useful if you want to store the carts conditions on a Livewire component since by default we have collections inside collections for conditions which Livewire does not support.

```
$cartConditions = Cart::getConditions(true);
$cartConditions = Cart::getConditions(active: true);

foreach ($cartConditions as $condition) {
    $condition['name']; // the name of the condition
    $condition['type']; // the type
    $condition['value']; // the value of the condition
    $condition['order']; // the order of the condition
    $condition['minimum']; // the minimum dollar amount of the target, needed to activate the condition
    $condition['maximum']; // the maximum dollar amount of the target, needed to keep the condition active
    $condition['attributes']; // the attributes of the condition, returns an empty [] if no attributes added
}
```

##### Getting conditions by name: **Cart::getCondition($conditionName)**

[](#getting-conditions-by-name-cartgetconditionconditionname)

```
$condition = Cart::getCondition('GST');

$condition->getTarget(); // the target of which the condition was applied
$condition->getName(); // the name of the condition
$condition->getType(); // the type
$condition->getValue(); // the value of the condition
$condition->getMinimum(); // the minimum dollar amount of the target, needed to activate the condition
$condition->getMaximum(); // the maximum dollar amount of the target, needed to keep the condition active
$condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
```

##### Getting active conditions on the cart: **Cart::getConditions(active: true)**

[](#getting-active-conditions-on-the-cart-cartgetconditionsactive-true)

You can get only active conditions by passsing `active: true` to the `getConditions()` method.

This will return conditions that are actively being applied to the cart (i.e if they meet their minimum or maximum value)

```
$tenPercentOff = new CartCondition([
    'name' => '10% Off',
    'type' => 'discount',
    'target' => 'subtotal',
    'value' => '-10%',
    'minimum' => 120,
    'order' => 1,
]);

Cart::getConditions(active: true);

// will return "10% Off" if the subtotal of the cart is $200.
// will return no conditions if the subtotal is $100.

$shipping = new CartCondition([
    'name' => 'Shipping',
    'type' => 'discount',
    'target' => 'subtotal',
    'value' => '10',
    'maximum' => 200,
    'order' => 1,
]);

Cart::getConditions(active: true);

// will return "Shipping" if the subtotal of the cart is less than or equal 200
// will return no conditions if the subtotal is $210
```

##### Calculating condition value

[](#calculating-condition-value)

There are 2 ways to calculate the value of a condition:

1. Using the `getCalculatedValue` method on the condition instance
2. Using the `getCalculatedValueForCondition` method on the cart instance and passing the condition name

##### Using the `getCalculatedValue` method on the condition instance

[](#using-the-getcalculatedvalue-method-on-the-condition-instance)

```
$subTotal = Cart::getSubTotal();
$condition = Cart::getCondition('10% GST');
$conditionCalculatedValue = $condition->getCalculatedValue($subTotal);
```

##### Using the `getCalculatedValueForCondition` method on the cart instance

[](#using-the-getcalculatedvalueforcondition-method-on-the-cart-instance)

This method automatically calculates the value of a condition by it's name based on the order of the conditions.

```
Cart::add([
    'id' => 1,
    'name' => 'Apple iPhone 15',
    'price' => 200,
    'quantity' => 1,
    'attributes' => [],
]);

$couponDiscount = new CartCondition([
    'name' => 'Coupon Discount',
    'type' => 'discount',
    'target' => 'subtotal',
    'value' => '-200',
    'order' => 1,
]);

$giftCard = new CartCondition([
    'name' => 'Gift Card',
    'type' => 'discount',
    'target' => 'subtotal',
    'value' => '-200',
    'order' => 2,
]);

Cart::getCalculatedValueForCondition('Coupon Discount'); // returns 200
Cart::getCalculatedValueForCondition('Gift Card'); // returns 0 as the coupon discount is applied first and brings the subtotal to 0
```

##### Adding conditions that activate once a minimum value is met

[](#adding-conditions-that-activate-once-a-minimum-value-is-met)

You can add a `minimum` amount required for a condition to activate.

This is useful for applying discounts only after certain cart values, i.e: 10% off for any purchases over $120.

```
$tenPercentOff = new CartCondition([
    'name' => '10% Off',
    'type' => 'discount',
    'target' => 'subtotal',
    'value' => '-10%',
    'minimum' => 120,
    'order' => 1,
]);

Cart::condition($tenPercentOff)
```

##### Adding conditions that only activate up to a maximum value

[](#adding-conditions-that-only-activate-up-to-a-maximum-value)

You can add a `maximum` amount required for a condition to activate.

This is useful for applying discounts up until amounts, i.e shipping for anything below $200, and free shipping above.

```
$shipping = new CartCondition([
    'name' => 'Shipping',
    'type' => 'discount',
    'target' => 'subtotal',
    'value' => '12',
    'maximum' => 200,
    'order' => 1,
]);

Cart::condition($shipping)
```

#### Conditions on items

[](#conditions-on-items)

Item conditions are useful if you have discounts to be applied specifically on an item and not on the whole cart value.

```
// lets create first our condition instance
$saleCondition = new \Wearepixel\Cart\CartCondition([
    'name' => '50% Off',
    'type' => 'tax',
    'value' => '-50%',
]);

// Create the product data with the condition
$product = [
    'id' => 456,
    'name' => 'Sample Item 1',
    'price' => 100,
    'quantity' => 1,
    'attributes' => [],
    'conditions' => $saleCondition
];

// Now add the product to the cart
Cart::add($product);

// You can of course also do multiple conditions on an item
$saleCondition = new \Wearepixel\Cart\CartCondition([
    'name' => 'SALE 5%',
    'type' => 'sale',
    'value' => '-5%',
]);

$discountCode = new CartCondition([
    'name' => 'Discount Code',
    'type' => 'promo',
    'value' => '-25',
]);

$item = [
    'id' => 456,
    'name' => 'Sample Item 1',
    'price' => 100,
    'quantity' => 1,
    'attributes' => [],
    'conditions' => [$saleCondition, $discountCode]
];

Cart::add($item);
```

##### Limiting a condition to a specific quantity of items

[](#limiting-a-condition-to-a-specific-quantity-of-items)

By default, an item condition applies to every unit of the item. Use `applies_to` to limit it to the first N units:

```
// 5% off the first item only - for 2x $50 items: (50*0.95) + 50 = $97.50
$firstItemDiscount = new CartCondition([
    'name' => '5% Off First Item',
    'type' => 'discount',
    'value' => '-5%',
    'applies_to' => 1,
]);

Cart::add([
    'id' => 1,
    'name' => 'Widget',
    'price' => 50.00,
    'quantity' => 2,
    'attributes' => [],
    'conditions' => $firstItemDiscount,
]);

Cart::getSubTotal(); // 97.50
```

When combining conditions, those without `applies_to` apply to every unit while those with `applies_to` only apply to the first N:

```
$limitedDiscount = new CartCondition(['name' => '10% Off First', 'type' => 'discount', 'value' => '-10%', 'applies_to' => 1]);
$globalDiscount  = new CartCondition(['name' => '5% Off All',   'type' => 'discount', 'value' => '-5%']);

// Item 1: 100 * 0.90 * 0.95 = 85.50
// Item 2: 100 * 0.95        = 95.00
// Subtotal: 180.50
```

> 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.

```
// 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 a condition to an existing item on the cart: **Cart::addItemCondition($productId, $itemCondition)**

[](#add-a-condition-to-an-existing-item-on-the-cart-cartadditemconditionproductid-itemcondition)

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

```
$condition = new CartCondition([
    'name' => 'COUPON 101',
    'type' => 'coupon',
    'value' => '-5%',
]);

Cart::addItemCondition(456, $condition);
```

#### Clearing Cart Conditions: **Cart::clearCartConditions()**

[](#clearing-cart-conditions-cartclearcartconditions)

This clears all cart level conditions, and does not affect item level conditions.

```
Cart::clearCartConditions()
```

If you wish to clear all conditions from all items and the cart, use **Cart::clearAllConditions()**

```
Cart::clearAllConditions()
```

##### Remove a specific cart condtion: **Cart::removeCartCondition($conditionName)**

[](#remove-a-specific-cart-condtion-cartremovecartconditionconditionname)

```
Cart::removeCartCondition('Summer Sale 5%')
```

##### Remove a specific item condition: **Cart::removeItemCondition($itemId, $conditionName)**

[](#remove-a-specific-item-condition-cartremoveitemconditionitemid-conditionname)

```
Cart::removeItemCondition(456, 'SALE 5%')
```

##### Clear all item conditions: **Cart::clearItemConditions($itemId)**

[](#clear-all-item-conditions-cartclearitemconditionsitemid)

```
Cart::clearItemConditions(456)
```

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

[](#get-conditions-by-type-cartgetconditionsbytypetype)

This returns all conditions that has been added to the cart by the type specified.

```
$tax = Cart::getConditionsByType('tax');
```

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

[](#remove-conditions-by-type-cartremoveconditionsbytypetype)

```
Cart::removeConditionsByType('tax');
```

### Cart Items

[](#cart-items)

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

Apart from the above methods, you can also get the price of an item with or without **item level conditions** applied.

These methods do not apply cart level conditions.

```
// With no conditions, just the price * quantity
$item->getPriceSum();

// With conditions applied get the price of a single quantity
$item->getPriceWithConditions();

// Get the sum with conditions applied
$item->getPriceSumWithConditions();

// Without conditions applied
$item->getPriceSumWithConditions();
```

### Storage Drivers

[](#storage-drivers)

The cart supports multiple storage backends via a driver system.

#### Session (default)

[](#session-default)

Cart data is stored in the Laravel session. No configuration needed.

#### Database

[](#database)

Store the cart in a database table for persistence across sessions.

**1. Create a migration:**

```
$table->string('session_id');
$table->text('items');
$table->text('conditions');
```

**2. Add casts to your model:**

```
protected $guarded = [];

protected $casts = [
    'items'      => 'array',
    'conditions' => 'array',
];
```

**3. Update `config/cart.php`:**

```
'driver' => 'database',

'drivers' => [
    'database' => [
        'model'      => \App\Models\Cart::class,
        'id'         => 'session_id',
        'items'      => 'items',
        'conditions' => 'conditions',
    ],
],
```

#### Redis

[](#redis)

Store the cart in Redis with an optional TTL.

```
'driver' => 'redis',

'drivers' => [
    'redis' => [
        'connection' => 'default',
        'ttl'        => 604800, // 7 days in seconds
    ],
],
```

#### Multi-driver

[](#multi-driver)

Write to multiple drivers simultaneously. Reads come from the first driver listed.

```
'driver' => 'multi',

'drivers' => [
    'multi' => ['session', 'database'],
],
```

#### Custom driver

[](#custom-driver)

Generate a custom driver stub with `php artisan cart:make:driver MyDriver` then register it in `CartManager`.

### Artisan Generators

[](#artisan-generators)

#### Install: **cart:install**

[](#install-cartinstall)

Publishes the config file and scaffolds `app/Cart/` with subdirectories for your coupons, tax rules, shipping rates, and custom drivers.

```
php artisan cart:install
```

#### Generate a Coupon: **cart:make:coupon**

[](#generate-a-coupon-cartmakecoupon)

```
php artisan cart:make:coupon TenPercentOff
```

Creates `app/Cart/Coupons/TenPercentOff.php` extending `Wearepixel\Cart\Coupons\Coupon`.

#### Generate a Tax Rule: **cart:make:tax**

[](#generate-a-tax-rule-cartmaketax)

```
php artisan cart:make:tax GstTaxRule
```

Creates `app/Cart/Tax/GstTaxRule.php` extending `Wearepixel\Cart\Tax\TaxRule`.

#### Generate a Shipping Rate: **cart:make:shipping**

[](#generate-a-shipping-rate-cartmakeshipping)

```
php artisan cart:make:shipping FlatRateShipping
```

Creates `app/Cart/Shipping/FlatRateShipping.php` extending `Wearepixel\Cart\Shipping\ShippingRate`.

#### Generate a Custom Driver: **cart:make:driver**

[](#generate-a-custom-driver-cartmakedriver)

```
php artisan cart:make:driver ElasticsearchDriver
```

Creates `app/Cart/Drivers/ElasticsearchDriver.php` implementing `CartDriver`.

#### Debug: **cart:debug**

[](#debug-cartdebug)

Dumps the current cart state. Not available in production.

```
php artisan cart:debug
```

### First-class Objects

[](#first-class-objects)

Instead of building raw `CartCondition` arrays, you can extend the provided base classes.

#### Coupon

[](#coupon)

```
namespace App\Cart\Coupons;

use Wearepixel\Cart\Coupons\Coupon;

class TenPercentOff extends Coupon
{
    protected string $code = 'SAVE10';
    protected string $value = '-10%';
    protected string $target = 'subtotal';

    public function isValid(): bool
    {
        return true; // add your own validation logic
    }
}

// Apply to the cart
Cart::coupon(new TenPercentOff);
```

#### TaxRule

[](#taxrule)

```
namespace App\Cart\Tax;

use Wearepixel\Cart\Tax\TaxRule;

class GstTaxRule extends TaxRule
{
    protected string $name = 'GST';
    protected string $value = '10%';
    protected string $target = 'subtotal';

    public function isApplicable(): bool
    {
        return true;
    }
}

Cart::tax(new GstTaxRule);
```

#### ShippingRate

[](#shippingrate)

```
namespace App\Cart\Shipping;

use Wearepixel\Cart\Shipping\ShippingRate;

class FlatRateShipping extends ShippingRate
{
    protected string $name = 'Standard Shipping';
    protected string $value = '+10';
    protected string $target = 'total';

    public function isApplicable(): bool
    {
        return true;
    }
}

Cart::shipping(new FlatRateShipping);
```

### Testing

[](#testing)

#### Cart::fake()

[](#cartfake)

`Cart::fake()` replaces the active driver with an in-memory `NullDriver` and returns a `CartFactory` for seeding test state.

```
use Wearepixel\Cart\Cart;

// In your test
$factory = app('cart')->fake();

$factory->withItems(3);
$factory->withCondition(new CartCondition(['name' => 'GST', 'type' => 'tax', 'value' => '10%', 'target' => 'subtotal']));

// Or chain it
app('cart')->fake()->withItems(2)->withCondition($condition);
```

#### Assertion Methods

[](#assertion-methods)

After `fake()`, the `Cart` instance exposes assertion methods:

```
$cart = app('cart');
$cart->fake();
$cart->add(1, 'Widget', 50.00, 2);

$cart->assertContains(1);            // item exists
$cart->assertCount(1);               // 1 distinct item
$cart->assertTotalQuantity(2);       // 2 units
$cart->assertSubTotal(100.00);       // subtotal
$cart->assertTotal(100.00);          // total
$cart->assertNotEmpty();             // cart has items
$cart->assertEmpty();                // cart is empty
$cart->assertConditionApplied('GST'); // condition present
```

### Livewire / Inertia

[](#livewire--inertia)

#### HasCart trait (Livewire)

[](#hascart-trait-livewire)

Add `HasCart` to any Livewire component to get reactive cart state and proxy methods.

```
use Wearepixel\Cart\Concerns\HasCart;

class CartComponent extends Component
{
    use HasCart;

    // $cartItems, $cartCount, $cartSubTotal, $cartTotal are auto-populated

    public function addToCart(int $id, string $name, float $price): void
    {
        $this->cartAdd($id, $name, $price, 1);
        // $cartCount, $cartSubTotal etc. update automatically
    }
}
```

#### ShareCartWithInertia (Inertia)

[](#sharecartwithinertia-inertia)

Share cart state on every Inertia response from your `HandleInertiaRequests` middleware:

```
use Wearepixel\Cart\Concerns\ShareCartWithInertia;

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'cart' => fn() => ShareCartWithInertia::data(),
    ]);
}
```

This shares `cart.items`, `cart.subtotal`, `cart.total`, and `cart.count` with every page.

### Events

[](#events)

The package provides a few events that you can listen to in order to manipulate the cart or take actions based on cart events.

#### LaravelCart.Created

[](#laravelcartcreated)

This fires every time a cart is instantiated (i.e every time \\Cart::add() is called)

```
Event::listen('LaravelCart.Added', function () {
    // cart was created
});

```

#### LaravelCart.Adding

[](#laravelcartadding)

This fires every time an item is being added to the cart

```
Event::listen('LaravelCart.Adding', function ($item) {
    // item is being added
});

```

#### LaravelCart.Added

[](#laravelcartadded)

This fires every time an item is successfully added to the cart

```
Event::listen('LaravelCart.Added', function ($item) {
    // item was added
});

```

#### LaravelCart.Updating

[](#laravelcartupdating)

This fires every time an item is being updated

```
Event::listen('LaravelCart.Updating', function ($item) {
    // item is being updated
});

```

#### LaravelCart.Updated

[](#laravelcartupdated)

This fires every time an item is successfully updated

```
Event::listen('LaravelCart.Updated', function ($item) {
    // item was updated
});

```

#### LaravelCart.Removing

[](#laravelcartremoving)

This fires every time an item is being removed

```
Event::listen('LaravelCart.Removing', function ($item) {
    // item is being removed
});

```

#### LaravelCart.Removed

[](#laravelcartremoved)

This fires every time an item is successfully removed

```
Event::listen('LaravelCart.Removed', function ($item) {
    // item was removed
});

```

#### LaravelCart.Clearing

[](#laravelcartclearing)

This fires every time the cart is being cleared

```
Event::listen('LaravelCart.Clearing', function () {
    // cart is being cleared
});

```

#### LaravelCart.Cleared

[](#laravelcartcleared)

This fires every time the cart is successfully cleared

```
Event::listen('LaravelCart.Cleared', function () {
    // cart was cleared
});

```

🫡 Credits
---------

[](#-credits)

This package was orignally created by [darryldecode](https://github.com/darryldecode) but has since seen almost no updates. I have decided to take the old package and transform it into a new package with new features and updates.

📓 License
---------

[](#-license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance97

Actively maintained with recent releases

Popularity40

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~30 days

Recently: every ~4 days

Total

23

Last Release

21d ago

Major Versions

1.2.1 → 2.0.02026-03-23

2.1.4 → 3.0.02026-06-13

PHP version history (4 changes)1.0.0PHP &gt;=8.0

1.0.3PHP &gt;=8.1

1.1.0PHP &gt;=8.2

2.0.0PHP &gt;=8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/80378407?v=4)[Pixel](/maintainers/wearepixel)[@wearepixel](https://github.com/wearepixel)

---

Top Contributors

[![joelwmale](https://avatars.githubusercontent.com/u/3906839?v=4)](https://github.com/joelwmale "joelwmale (126 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (9 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (4 commits)")[![pixel-workflows[bot]](https://avatars.githubusercontent.com/in/3240715?v=4)](https://github.com/pixel-workflows[bot] "pixel-workflows[bot] (1 commits)")

---

Tags

laravelcartshopping cart

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/wearepixel-laravel-cart/health.svg)

```
[![Health](https://phpackages.com/badges/wearepixel-laravel-cart/health.svg)](https://phpackages.com/packages/wearepixel-laravel-cart)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9762.4M131](/packages/roots-acorn)[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k102.4M1.4k](/packages/spatie-laravel-permission)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M151](/packages/laravel-mcp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
