PHPackages                             lachezargrigorov/laravel-shopping-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. [Framework](/categories/framework)
4. /
5. lachezargrigorov/laravel-shopping-cart

ActiveLibrary[Framework](/categories/framework)

lachezargrigorov/laravel-shopping-cart
======================================

An easy to use more advanced shopping cart for Laravel applications.

v1.1.1(8y ago)4321MITPHPPHP ^5.6|^7.0

Since Nov 6Pushed 8y ago1 watchersCompare

[ Source](https://github.com/lachezargrigorov/laravel-shopping-cart)[ Packagist](https://packagist.org/packages/lachezargrigorov/laravel-shopping-cart)[ Docs](https://github.com/lachezargrigorov/laravel-shopping-cart)[ RSS](/packages/lachezargrigorov-laravel-shopping-cart/feed)WikiDiscussions master Synced yesterday

READMEChangelog (4)Dependencies (6)Versions (5)Used By (0)

Laravel Shopping Cart
=====================

[](#laravel-shopping-cart)

[![Latest Stable Version](https://camo.githubusercontent.com/ed3108313bd17266e68d85daf66ccc7eacf89fba2eedd83dbc221367f84b7d32/68747470733a2f2f706f7365722e707567782e6f72672f6c616368657a6172677269676f726f762f6c61726176656c2d73686f7070696e672d636172742f762f737461626c65)](https://packagist.org/packages/lachezargrigorov/laravel-shopping-cart)[![Latest Unstable Version](https://camo.githubusercontent.com/37816bf8f6ae7b1d0a09cac8f1b64665c4ece96b85152d4683842a2eb718e53d/68747470733a2f2f706f7365722e707567782e6f72672f6c616368657a6172677269676f726f762f6c61726176656c2d73686f7070696e672d636172742f762f756e737461626c65)](https://packagist.org/packages/lachezargrigorov/laravel-shopping-cart)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/b82c2b207f25733719824d6ca999405474238381f7e22a4bf28f8fce0b505fb3/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6c616368657a6172677269676f726f762f6c61726176656c2d73686f7070696e672d636172742f6d61737465722e737667)](https://travis-ci.org/lachezargrigorov/laravel-shopping-cart)[![Total Downloads](https://camo.githubusercontent.com/f1c2a1334d64e970f4045e4e51a700718a23b30a94de77fb495644323f6edd0b/68747470733a2f2f706f7365722e707567782e6f72672f6c616368657a6172677269676f726f762f6c61726176656c2d73686f7070696e672d636172742f646f776e6c6f616473)](https://packagist.org/packages/lachezargrigorov/laravel-shopping-cart)

An easy to use more advanced shopping cart for Laravel applications.

Installation
------------

[](#installation)

### Via Composer

[](#via-composer)

```
$ composer require lachezargrigorov/laravel-shopping-cart
```

Laravel 5.5 and above uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider and Facade to the array in config/app.php

If you don't use auto-discovery, add the CartServiceProvider to the providers array in config/app.php

```
\Lachezargrigorov\Cart\CartServiceProvider::class,
```

If you want to use the Cart facade, add this to the aliases array in app.php:

```
'Cart' => \Lachezargrigorov\Cart\Facades\Cart::class,
```

Implement the Item interface in your product model. The Cart and Item uses ***getCartPrice*** method to calculate the totals.

```
use Illuminate\Database\Eloquent\Model;
use Lachezargrigorov\Cart\Iterfaces\Item;

class Product extends Model implements Item {}
```

Publish the package config to your local config with the publish command and configure them:

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

Set the item\_class in local package config (cart.php)

```
"item_class" => \App\Product::class,
```

Legend
------

[](#legend)

#### Elements

[](#elements)

- Cart : class Lachezargrigorov\\Cart\\Cart
- Item : class Lachezargrigorov\\Cart\\Item
- Condition : abstract class Lachezargrigorov\\Cart\\Condition;
- ItemCondition : class Lachezargrigorov\\Cart\\ItemCondition extends Condition
- CartCondition : class Lachezargrigorov\\Cart\\CartCondition extends Condition

#### Interfaces

[](#interfaces)

- Item : interface Lachezargrigorov\\Cart\\Interfaces\\Item

#### Collections

[](#collections)

- LaravelCollection : class Illuminate\\Support\\Collection
- Collection : class Lachezargrigorov\\Cart\\Collections\\Collection extends LaravelCollection
- ItemAttributesCollection : class Lachezargrigorov\\Cart\\Collections\\ItemAttributesCollection extends Collection
- ConditionAttributesCollection : class Lachezargrigorov\\Cart\\Collections\\ConditionAttributesCollection extends Collection

#### Exceptions

[](#exceptions)

- CartException : class Lachezargrigorov\\Cart\\Exceptions\\CartException

Usage`
------

[](#usage)

### Cart

[](#cart)

#### Item

[](#item)

##### Add or get Item

[](#add-or-get-item)

This method add or get an Item if exist.

- $id : int - the id of the item (product) model
- return : Item

```
Cart::item($id);  //quantity = 0 on create
```

##### Models loading process

[](#models-loading-process)

For better performance models are lazy associated to the items on first '$item-&gt;model' call after init or item addition in single DB request so you don't need to add any extra data like name, price, etc.

```
Cart::item(1);
Cart::item(2)->addQuantity(1);

//models are not loaded yet

//models are lazy loaded here
Cart::item(1)->model;

//if item not exist already, add a new one and mark that models need to be loaded again on next "$item->model" call
Cart::item(3);
Cart::item(4);

//models are not loaded again

//models are lazy loaded here again
Cart::item(4)->model;
```

##### Remove Item

[](#remove-item)

-return : removed Item

```
Cart::item($id)->remove();
```

##### Get items

[](#get-items)

- return : LaravelCollection with Items

```
Cart::items();
```

##### Has item (Item)

[](#has-item-item)

- id : int - the id of the item (product) model
- return : bool

```
Cart::has($id);
```

##### Get items count

[](#get-items-count)

- return : int

```
Cart::count();
```

##### Remove items (Item)

[](#remove-items-item)

- ids : \[\] - array with ids
- return : Cart

```
Cart::remove($ids);
```

##### Remove all cart items

[](#remove-all-cart-items)

- return : Cart

```
Cart::empty();
```

##### Is empty for items

[](#is-empty-for-items)

- return : bool

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

##### Get item keys

[](#get-item-keys)

- return : LaravelCollection with keys

```
Cart::keys();
```

##### Empty the item models

[](#empty-the-item-models)

This will case the cart to reload the models on next model call.

- return : Cart

```
Cart::emptyModels();
```

#### CartCondition

[](#cartcondition)

##### Add or get CartCondition

[](#add-or-get-cartcondition)

This method add or get an CartCondition if exist.

- name : condition name
- return : CartCondition

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

#### Add or set CartConditions as array

[](#add-or-set-cartconditions-as-array)

This will rewrite the existing conditions.

- return : Cart

```
    Cart::setConditionAsArray([
               "name"  => "all sale1",
               "type"  => "all sale",
               "value" => "-10%",
           ]);

    //or as multidimensional array

  Cart::setConditionAsArray([
            [
                "name"  => "all sale1",
                "type"  => "all sale",
                "value" => "-10%",
            ],
            [
                "name"  => "all sale2",
                "type"  => "all sale",
                "value" => "+1",
            ],
        ]);
```

##### Get conditions

[](#get-conditions)

- return : LaravelCollection with CartConditions

```
Cart::conditions();
```

##### Has condition

[](#has-condition)

- name : condition name
- return : bool

```
Cart::hasCondition($name);
```

##### Get conditions count

[](#get-conditions-count)

- return : int

```
Cart::countConditions();
```

##### Remove conditions

[](#remove-conditions)

- names : array - array with names
- return : Cart

```
Cart::removeConditions($names);
```

##### Remove all cart conditions

[](#remove-all-cart-conditions)

- return : Cart

```
Cart::emptyConditions();
```

##### Is empty for conditions

[](#is-empty-for-conditions)

- return : bool

```
Cart::isEmptyConditions();
```

##### Get condition keys (names)

[](#get-condition-keys-names)

- return : LaravelCollection with keys

```
Cart::keysOfConditions();
```

#### Total methods

[](#total-methods)

##### Get total quantity

[](#get-total-quantity)

- return : int

```
Cart::totalQuantity();
```

##### Get cart subtotal without applied ItemConditions and CartConditions

[](#get-cart-subtotal-without-applied-itemconditions-and-cartconditions)

- return : double

```
Cart::subtotalWithoutConditions();
```

##### Get cart subtotal without applied CartConditions

[](#get-cart-subtotal-without-applied-cartconditions)

- return : double

```
Cart::subtotal();
```

##### Get cart total with applied ItemConditions and CartConditions

[](#get-cart-total-with-applied-itemconditions-and-cartconditions)

- return : double

```
Cart::total();
```

### Item

[](#item-1)

#### Properties

[](#properties)

- id : int
- quantity : int
- attributes : ItemAttributesCollection
- conditions : LaravelCollection with ItemConditions
- model : Illuminate\\Database\\Eloquent\\Model

##### Set quantity

[](#set-quantity)

- return : Item

```
Cart::item($id)->quantity(1);
```

##### Add quantity (current quantity + added quantity)

[](#add-quantity-current-quantity--added-quantity)

- return : Item

```
Cart::item($id)->addQuantity(1);
```

##### Get quantity

[](#get-quantity)

- return : int

```
Cart::item($id)->quantity;
```

##### Set attributes

[](#set-attributes)

- return : Item

```
Cart::item($id)->attributes(["size" => "L", "color" => "blue"]);
```

##### Get attributes

[](#get-attributes)

- return : ItemAttributesCollection

```
$attributesCollection = Cart::item($id)->attributes;
$itemSize = $attributesCollection->size;
$itemColor = $attributesCollection->color;

 //or

Cart::item($id)->attributes->size;
Cart::item($id)->attributes->color;

//or using LaravelCollection methods

Cart::item($id)->attributes->has("size");
Cart::item($id)->attributes->get("size");
Cart::item($id)->attributes->each(function($value, $key){
    ...
});
```

##### Empty attributes (delete all attributes)

[](#empty-attributes-delete-all-attributes)

- return : Item

```
Cart::item($id)->emptyAttributes();
```

##### Add or get ItemCondition

[](#add-or-get-itemcondition)

- name : string - condition name
- return : ItemCondition

```
Cart::item($id)->condition($name);
```

#### Add or set ItemConditions as array

[](#add-or-set-itemconditions-as-array)

This will rewrite the existing conditions.

- return : Item

```
    Cart::item(1)->setConditionAsArray([
               "name"  => "item sale1",
               "type"  => "item sale",
               "value" => "-10%",
           ]);

    //or as multidimensional array

  Cart::item(1)->setConditionAsArray([
            [
                "name"  => "item sale1",
                "type"  => "item sale",
                "value" => "-10%",
            ],
            [
                "name"  => "item sale2",
                "type"  => "item sale",
                "value" => "+1",
            ],
        ]);
```

##### Get conditions

[](#get-conditions-1)

- return : LaravelCollection with ItemConditions

```
Cart::item($id)->conditions();
```

##### Remove ItemCondition

[](#remove-itemcondition)

return : removed ItemCondition

```
Cart::item($id)->condition($name)->remove();
```

##### Has condition

[](#has-condition-1)

- name : string - condition name
- return : bool

```
Cart::item($id)->hasCondition($name);
```

##### Does item contain any conditions

[](#does-item-contain-any-conditions)

- return : bool

```
Cart::item($id)->isEmptyConditions();
```

##### Empty conditions (remove all conditions)

[](#empty-conditions-remove-all-conditions)

- return : Item

```
Cart::item($id)->emptyConditions();
```

##### Get model

[](#get-model)

- return : Illuminate\\Database\\Eloquent\\Model

```
Cart::item($id)->model;
```

#### Price methods

[](#price-methods)

##### Get price without applied conditions

[](#get-price-without-applied-conditions)

-return : double

```
Cart::item($id)->priceWithoutConditions();
```

##### Get price sum without applied conditions (price \* quantity)

[](#get-price-sum-without-applied-conditions-price--quantity)

-return : double

```
Cart::item($id)->priceSumWithoutConditions();
```

##### Get price with applied conditions

[](#get-price-with-applied-conditions)

-return : double

```
Cart::item($id)->price();
```

##### Get price sum with applied conditions (price \* quantity)

[](#get-price-sum-with-applied-conditions-price--quantity)

-return : double

```
Cart::item($id)->priceSum();
```

#### Set helper

[](#set-helper)

- \[\] : array - quantity : int (not required), add\_quantity : int (not required), attributes : array (not required), conditions : array (not required)
- return : Item

```
  Cart::item(1)->set([
            "quantity" => 1,
            //"add_quantity" => 2,
            "attributes" => [
                "size" => "S",
            ],
            "conditions" => [
                [
                    "name"  => "whole sale",
                    "type"  => "all sale",
                    "value" => "10%",
                    "attributes" => ["some" => "attribute"]
                ], [
                    "name"  => "item sale",
                    "type"  => "item sale",
                    "value" => "-1",
                ]
            ]

        ]);

    // equel to

   Cart::item(1)->quantity(1)/*->addQuantity(2)*/->attributes(["size" => "S"])->condition('whole sale')->type("all sale")->value("10%")->attributes(["some" => "attribute"]);
   Cart::item(1)->condition("item sale")->type("item sale")->value("-1");
```

### Condition (CartCondition and ItemCondition)

[](#condition-cartcondition-and-itemcondition)

#### Properties

[](#properties-1)

- name : string - condition name and it's collection key are always the same
- type : string
- value : string - \[+-\]?\[0-9\]+(\\.\[0-9\]+)?\[%\]? examples: +10%, 10%, 10.22% -10%, +10.11, -10.80
- attributes : ConditionAttributesCollection

##### Set name

[](#set-name)

This will change the key in collection too.

- name : string
- return : CartCondition | ItemCondition

```
// CartCondition

//this will create a new condition with name and key = "all sale"
Cart::condition("all sale");

//this will change the name and the key in collection too
Cart::condition("all sale")->name("friday sale");

//now this condition is accessible with the new key (name)
Cart::condition("friday sale");

// ItemCondition

//this will create a new item condition with name and key = "all sale"
Cart::item(1)->condition("all sale");

//this will change the name and the key in collection too
Cart::item(1)->condition("all sale")->name("friday sale");

//now this item condition is accessible with the new key
Cart::item(1)->condition("friday sale");
```

##### Get name

[](#get-name)

- return : string

```
// CartCondition

Cart::condition("all sale")->name;

// ItemCondition

Cart::item(1)->condition("item sale")->name;
```

##### Set type

[](#set-type)

- type : string
- return : CartCondition | ItemCondition

```
// CartCondition

Cart::condition("all sale")->type($type);

// ItemCondition

Cart::item(1)->condition("item sale")->type($type);
```

##### Get type

[](#get-type)

- return : string

```
// CartCondition

Cart::condition("all sale")->type;

// ItemCondition

Cart::item(1)->condition("item sale")->type;
```

##### Set value

[](#set-value)

- value : string
- return : CartCondition | ItemCondition

```
// CartCondition

Cart::condition("all sale")->value($value);

// ItemCondition

Cart::item(1)->condition("item sale")->value($value);
```

##### Get value

[](#get-value)

- return : string

```
// CartCondition

Cart::condition("all sale")->value;

// ItemCondition

Cart::item(1)->condition("item sale")->value;
```

##### Set attributes

[](#set-attributes-1)

Merge existing attributes.

- attributes : array
- return : CartCondition | ItemCondition

```
// CartCondition

$attributes = ["some_attribute" => "attribute"];
Cart::condition("all sale")->attributes($attributes);

// ItemCondition

Cart::item(1)->condition("item sale")->attributes($attributes);
```

##### Get attributes

[](#get-attributes-1)

- return : ConditionAttributesCollection

```
// CartCondition

Cart::condition("all sale")->attributes;
Cart::condition("all sale")->attributes->some_attribute;

// ItemCondition

Cart::item(1)->condition("item sale")->attributes;
Cart::item(1)->condition("item sale")->attributes->some_attribute;

//or useing any LaravelCollection method
Cart::item(1)->condition("item sale")->attributes->get("some_attribute");
```

##### Empty attributes (delete all attributes)

[](#empty-attributes-delete-all-attributes-1)

- return : CartCondition | ItemCondition

```
// CartCondition

Cart::condition("all sale")->emptyAttributes();

// ItemCondition

Cart::item(1)->condition("item sale")->emptyAttributes();
```

#### Set helper

[](#set-helper-1)

- \[\] : array - name : string (not required), type : string (not required), value : string (not required), attributes : array (not required)
- return : CartCondition | ItemCondition

```
// CartCondition

Cart::condition("all sale")->set([
    "name" => "sale",
    "type" => "sale",
    "value" => "-10%",
    "attributes" => [
        "size" => "M"
    ]
]);

// ItemCondition

Cart::item(1)->condition("all sale")->set([
    "name" => "sale",
    "type" => "sale",
    "value" => "-10%",
    "attributes" => [
        "size" => "M"
    ]
]);
```

Testing
-------

[](#testing)

```
$ composer test
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md), [ISSUE\_TEMPLATE](ISSUE_TEMPLATE.md), [PULL\_REQUEST\_TEMPLATE](PULL_REQUEST_TEMPLATE.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Lachezar Grigorov](http://grigorov.website)

License
-------

[](#license)

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

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~5 days

Total

4

Last Release

3091d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d7a7ac715702b10efa5f10d1e83f51b2aecaa69a1747fa64e8d5cbe11c4ec268?d=identicon)[lachezargrigorov](/maintainers/lachezargrigorov)

---

Top Contributors

[![la40](https://avatars.githubusercontent.com/u/3510976?v=4)](https://github.com/la40 "la40 (14 commits)")

---

Tags

laravelcartshopping cartlachezargrigorov

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[darryldecode/cart

Laravel 5 Shopping cart

1.4k1.7M7](/packages/darryldecode-cart)[lukepolo/laracart

A simple cart for Laravel

583135.4k1](/packages/lukepolo-laracart)[treestoneit/shopping-cart

An easy-to-use shopping cart for Laravel

6263.0k2](/packages/treestoneit-shopping-cart)

PHPackages © 2026

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