PHPackages                             lombervid/shoppingcart - 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. lombervid/shoppingcart

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

lombervid/shoppingcart
======================

A simple shopping cart class for PHP

v3.0.0(2y ago)113854MITPHPPHP ^8.1

Since Jun 1Pushed 2y ago2 watchersCompare

[ Source](https://github.com/lombervid/shoppingcart)[ Packagist](https://packagist.org/packages/lombervid/shoppingcart)[ Docs](https://github.com/lombervid/shoppingcart)[ RSS](/packages/lombervid-shoppingcart/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

[![GitHub release (latest SemVer)](https://camo.githubusercontent.com/6164a0d94f9afb312f6d0adad4c64f74593b50a7f789f5f4bfc806fb8a212d55/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6c6f6d6265727669642f73686f7070696e67636172743f646973706c61795f6e616d653d74616726736f72743d73656d766572)](https://github.com/lombervid/shoppingcart/releases/latest)[![Packagist](https://camo.githubusercontent.com/f427bc5fd8ca304b57e88e744a04fe05d7d1f79780a7cc34a57d809b33418b86/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f6d6265727669642f73686f7070696e6763617274)](https://packagist.org/packages/lombervid/shoppingcart)[![PHP Version](https://camo.githubusercontent.com/6821aebfdfd0f4dcdce8d5f0691398e4dfe99cbfa3700dbc33a2375e1252ccd0/68747470733a2f2f706f7365722e707567782e6f72672f6c6f6d6265727669642f73686f7070696e67636172742f726571756972652f706870)](https://camo.githubusercontent.com/6821aebfdfd0f4dcdce8d5f0691398e4dfe99cbfa3700dbc33a2375e1252ccd0/68747470733a2f2f706f7365722e707567782e6f72672f6c6f6d6265727669642f73686f7070696e67636172742f726571756972652f706870)[![tests](https://github.com/lombervid/shoppingcart/actions/workflows/tests.yml/badge.svg)](https://github.com/lombervid/shoppingcart/actions/workflows/tests.yml?query=branch%3Amain)[![GitHub](https://camo.githubusercontent.com/974f647fd43fcfae3713667eaf9aad3b01bed33f69b97b4f47075fd208c96c2a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c6f6d6265727669642f73686f7070696e6763617274)](https://github.com/lombervid/shoppingcart/blob/main/LICENSE)

ShoppingCart PHP Class
======================

[](#shoppingcart-php-class)

***ShoppingCart*** is a simple *PHP* package that provides you with a simple shopping cart implementation stored in `session`.

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

[](#installation)

### Composer

[](#composer)

You can install it using [composer](https://getcomposer.org/):

```
composer require lombervid/shoppingcart
```

Usage
-----

[](#usage)

Create an instance of `ShoppingCart` class.

```
use Lombervid\ShoppingCart\ShoppingCart;

$shoppingCart = new ShoppingCart();
```

### Add items

[](#add-items)

You can add items calling the method `add()` passing an `Item` instance as parameter.

```
use Lombervid\ShoppingCart\Item;
use Lombervid\ShoppingCart\ShoppingCart;

$cart = new ShoppingCart();
$cart->add(new Item('1', 'Cake', 15.56));
$cart->add(new Item('15', 'Frappe', 5));

foreach ($cart->items() as $item) {
	// $item->id
	// $item->name
}
```

at this point your `$cart->items()` will look like this:

```
array:2 [▼
  1 => Lombervid\ShoppingCart\Item {#5 ▼
    -id: "1"
    -name: "Cake"
    -price: 15.56
  }
  15 => Lombervid\ShoppingCart\Item {#6 ▼
    -id: "15"
    -name: "Frappe"
    -price: 5.0
  }
]
```

### Add extra fields to your item

[](#add-extra-fields-to-your-item)

You can also add extra fields (such as price, name, etc) to your item. The `Item` constructor receives a parameter `fields` which is an `Array` with the following structure:

```
[
    'field_name'   => 'field_value',
    'field_2_name' => 'field_2_value'
]
```

when you provide the `$fields` param, each field of the array is added to your item.

```
$fields = [
	'size'  => 'XL',
	'color' => 'blue'
];

$item = new Item('23', 'My Shirt', 2.5, fields: $fields);
$cart->add($item);
```

with the above code your `$cart->items()` will look line:

```
array:1 [▼
  23 => Lombervid\ShoppingCart\Item {#5 ▼
    -id: "23"
    -name: "My Shirt"
    -price: 2.5
    -qty: 1
    -fields: array:2 [▼
      "size" => "XL"
      "color" => "blue"
    ]
  }
]
```

Then you can access any extra field as if they were properties:

```
foreach ($cart->items() as $item) {
    // $item->size
    // $item->color
}
```

### Remove items

[](#remove-items)

You can remove an item from the cart calling the method `remove($id)` which receive item's `$id` as parameter.

```
$cart->remove(23);
```

### Clear the cart

[](#clear-the-cart)

You can clear the cart calling the method `clear()` which removes all the items from the cart.

```
$shoppingCart->clear();
```

Advanced options
----------------

[](#advanced-options)

### ShoppingCart

[](#shoppingcart)

#### Cart options

[](#cart-options)

It is an `array` of options. The default value is:

```
[
    'name'     => 'shopping_cart',
    'autosave' => true,
    'tax'      => 0,
    'shipping' => [
        'amount' => 0,
        'free'   => 0,
    ],
]
```

OptionTypeDefaultDescription`name``string``shopping_cart`Cart's name. Used to save the cart in storage`autosave``bool``true`If set to `true`, cart is saved when object is destroyed`tax``int|float``0`Porcentaje to be used as tax (0 - 100)`shipping.amount``int|float``0`Shipping cost`shipping.free``int|float``0`Value after which shipping will be free. `0` to disable#### Constructor

[](#constructor)

ParameterTypeDefaultRequiredDescription`$options``array`See [Cart options](#cart-options)`false`Cart options`$storage``StorageInterface``NativeSessionStorage``false`Storage driver#### Methods

[](#methods)

NameDescription`add(Item $item, bool $append = true): void`Add an item to the cart`remove(string $id): bool`Remove an item from the cart`subtotal(): float`Get subtotal`shipping(): float`Get shipping cost`tax(): float`Get tax`total(): float`Get total`inCart(string $id): bool`Check if an item is in the cart`items(): array`Return the items in the cart`clear(): static`Remove all items from the cart`isEmpty(): bool`Check if the cart is empty`totalItems(): int`Return the total (distinct) items in the cart`save(): void`Save items in the storage`toArray(): array`Return items as `array`### Item

[](#item)

#### Constructor

[](#constructor-1)

ParameterTypeDefaultRequiredDescription`$id``string``N/A``true`Identifier`$name``string``N/A``true`Name / description`$price``float``N/A``true`Price (`>= 0`)`$qty``int``1``false`Quantity (`> 0`)`$fields``array``[]``false`Extra fields`$discount``float``0``false`Discount (`>= 0`)#### Methods

[](#methods-1)

NameDescription`add(int $qty): void`Increase item's quantity by `$qty``update(int $qty): void`Update item's quantity to `$qty``get(string $name, mixed $default = null): mixed`Get `$name` property/field`hasDiscount(): bool`Check if has a discount`price(): float`Get item's price`total(): float`Get total`toArray(): array`Return item as `array`Contributing
------------

[](#contributing)

Refer to [CONTRIBUTING](./.github/CONTRIBUTING.md) for information.

License
-------

[](#license)

[MIT](https://github.com/lombervid/shoppingcart/blob/main/LICENSE)

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

1076d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c7a9845e792d123523dd59f449c8c38dc920b8af470be7683165ead3f5e499b?d=identicon)[lombervid](/maintainers/lombervid)

---

Top Contributors

[![lombervid](https://avatars.githubusercontent.com/u/3432651?v=4)](https://github.com/lombervid "lombervid (71 commits)")

---

Tags

composerphp81shoppingcartshoppingcart-libraryshopping cart

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lombervid-shoppingcart/health.svg)

```
[![Health](https://phpackages.com/badges/lombervid-shoppingcart/health.svg)](https://phpackages.com/packages/lombervid-shoppingcart)
```

###  Alternatives

[extcode/cart

Shopping Cart(s) for TYPO3

57119.3k14](/packages/extcode-cart)[wearepixel/laravel-cart

A cart implementation for Laravel

1310.5k](/packages/wearepixel-laravel-cart)[ultrono/laravelshoppingcart-1

Laravel 11 and above Shopping cart

1110.0k](/packages/ultrono-laravelshoppingcart-1)

PHPackages © 2026

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