PHPackages                             jamesdb/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. jamesdb/cart

ActiveLibrary[Framework](/categories/framework)

jamesdb/cart
============

A framework agnostic shopping cart package.

0.3.0(9y ago)948MITPHPPHP ^5.5.0 || ^7.0

Since Sep 28Pushed 8y ago3 watchersCompare

[ Source](https://github.com/jamesdb/cart)[ Packagist](https://packagist.org/packages/jamesdb/cart)[ Docs](http://github.com/jamesdb/cart)[ RSS](/packages/jamesdb-cart/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (18)Used By (0)

Shopping Cart
=============

[](#shopping-cart)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/c125c97970c1d3679c162aa556b3cee264c5defb618a33424a39d596f1358e7b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a616d657364622f636172742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/jamesdb/cart)[![Coverage Status](https://camo.githubusercontent.com/edf15273368ef54b30c36ee2ef3c0e43c640ab0e62ef1d223aee105e80547678/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6a616d657364622f636172742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/jamesdb/cart/code-structure)[![Quality Score](https://camo.githubusercontent.com/668f568819432cac8cc28e647d4856e839075683901b1f569736f743c917565a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6a616d657364622f636172742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/jamesdb/cart)[![Total Downloads](https://camo.githubusercontent.com/12b0fbccfd28e0a0f38907efa1fad9ed7999e585b3bc53d4d3a0f875a02cf0e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a616d657364622f636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jamesdb/cart)

A framework agnostic shopping cart package.

Install
-------

[](#install)

Via Composer

```
$ composer require jamesdb/cart
```

Notes
-----

[](#notes)

### Money Value Objects

[](#money-value-objects)

This package uses [moneyphp/money](https://github.com/moneyphp/money) (an implementation of Martin Fowler's money pattern). Floats are avoided due to them being ill-suited for monetary values. More information can be found in the links below:

-
-

Due to this when dealing with monetary values you will need to represent them as integers instead of floats.

An example of this can be found below.

```
use jamesdb\Cart\CartItem;

$item = new CartItem([
    ...
    'price' => 1099, // Instead of £10.99 or $10.99 etc.
    ...
]);
```

Setting Up
----------

[](#setting-up)

To setup a cart instance you need to pass an identifier and storage implementation to the cart constructor.

The currency defaults to 'GBP', if you want to change this you will need to pass your currency of choice into the `setCurrency` method as shown below.

A custom formatter callback can be setup via `setFormatterCallback`, see [moneyphp formatters](http://moneyphp.org/en/latest/features/formatting.html) for more information.

```
use jamesdb\Cart\Cart;
use jamesdb\Cart\Storage\NativeSessionDriver;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\DecimalMoneyFormatter;

$cart = new Cart('cart', new NativeSessionDriver);
$cart->setCurrency(new Currency('GBP'));

$cart->setFormatterCallback(function ($money) {
    $currencies = new ISOCurrencies();
    $moneyFormatter = new DecimalMoneyFormatter($currencies);

    return $moneyFormatter->format($money); // outputs in decimal format.
});
```

Any storage implementation can be used as long as it implements the `jamesdb\Cart\Storage\StorageInterface`.

Usage
-----

[](#usage)

### Adding Items

[](#adding-items)

`CartItem` implements `ArrayAccess` and uses the `__set` and `__get` magic methods to assign and access properties.

Added items will return a rowid.

```
use jamesdb\Cart\Cart;
use jamesdb\Cart\CartItem;

$cart = new Cart(...);

/**
 * Assign properties via __set.
 */
$item1 = new CartItem();
$item1->id = 2731;
$item1->name = 'Product';
$item1->price = 1099;
$item1->quantity = 1;

$cart->add($item1);

/**
 * ----------
 */

/**
 * Assign properties via ArrayAccess.
 */
$item2 = new CartItem([
    'id' => 2731,
    'name' => 'Product',
    'price' => 1099,
    'quantity' => 1
]);

$cart->add($item2)
```

### Updating Items

[](#updating-items)

You can either use the cart `update` method or access the properties directly on the `CartItem`.

Attempting to update an item that doesn't exist in the cart will result in a `CartItemUpdateException` being thrown.

```
$cart->update('rowid', ['name' => 'Renamed Product']);

/**
 * ----------
 */

$cartItem = $cart->find('rowid');
$cartItem->name = 'Renamed Product';
```

### Removing Items

[](#removing-items)

Attempting to remove an item that doesn't exist in the cart will result in a `CartItemRemoveException` being thrown.

```
$cart->remove('rowid');
```

### Clear the Cart

[](#clear-the-cart)

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

### Accessing a specific Item

[](#accessing-a-specific-item)

To access a specific item use the cart `getItem` method.

If an item can't be found the method will return `null`.

```
$cart->getItem('rowid');
```

### Accessing all Items

[](#accessing-all-items)

```
$cart->getItems();
```

### Filtering Items

[](#filtering-items)

The cart can be filtered by any supplied key and value with the `filter` method.

```
// Return all items with a quantity of 2.
$cart->filter('quantity', 2);

// Return all items with a price of 1000.
$cart->filter('price', 1000);
```

### Accessing Item counts

[](#accessing-item-counts)

The `getTotalUniqueItems` method will return an item count excluding quantities.

```
$cart->getTotalUniqueItems();
```

The `getTotalItems` method will return an item count including quantities.

```
$cart->getTotalItems();
```

A convenience method `isEmpty` is built into the cart, this proxies through to the getTotalUniqueItems with a === 0 check.

```
$cart->isEmpty();
```

### Accessing prices

[](#accessing-prices)

Get the overall price including tax with the `getTotalPrice` method.

```
$cart->getTotalPrice();
```

Occasionally dealing with tax isn't required, in this case you can use the `getTotalPriceExcludingTax` method.

```
$cart->getTotalPriceExcludingTax();
```

Get the total cart tax.

```
$cart->getTax();
```

Events
------

[](#events)

[League\\Event](http://event.thephpleague.com/2.0/) is built into the cart and provides a number of events that can be emitted, this allows you to easily hook into certain key points during the carts lifecycle.

You can subscribe to these events by attaching listeners to the cart via `addEventListener`.

All events have built in methods to retrieve the item triggering the event and the cart itself.

```
$cart->addEventListener('cart.add', function ($event) {
    $item = $event->getItem();
    $cart = $event->getCart();

    ...
});
```

### cart.add

[](#cartadd)

```
$cart->addEventListener('cart.add', function ($event) {});
```

### cart.update

[](#cartupdate)

```
$cart->addEventListener('cart.update', function ($event) {});
```

### cart.remove

[](#cartremove)

```
$cart->addEventListener('cart.remove', function ($event) {});
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.4% 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 ~26 days

Recently: every ~80 days

Total

17

Last Release

3466d ago

PHP version history (2 changes)0.1.0PHP &gt;= 5.4.0

0.3.0PHP ^5.5.0 || ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3a25b024ce5c910b1f77bec9ebfb646d5ac8177fa60191e4b18992e2adc9380d?d=identicon)[jamesdb](/maintainers/jamesdb)

---

Top Contributors

[![jamesdb](https://avatars.githubusercontent.com/u/6299056?v=4)](https://github.com/jamesdb "jamesdb (61 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

cartshopping

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[darryldecode/cart

Laravel 5 Shopping cart

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

A simple cart for Laravel

583135.4k1](/packages/lukepolo-laracart)[sylius/sylius-standard

Starting point for projects powered by Sylius eCommerce.

271291.3k](/packages/sylius-sylius-standard)[jackiedo/cart

A package used to create and manage carts (such as shopping, recently viewed, compared items...) in Laravel application.

20852.7k](/packages/jackiedo-cart)[binafy/laravel-cart

Laravel Cart is a customizable package for adding shopping cart functionality to Laravel applications

40445.9k](/packages/binafy-laravel-cart)[anam/phpcart

Simple framework agnostic shopping cart

12115.4k](/packages/anam-phpcart)

PHPackages © 2026

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