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

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

rogervila/cart
==============

PHP Shopping cart using Fowler's Money pattern

293[1 issues](https://github.com/rogervila/cart/issues)PHP

Since Jan 9Pushed 4y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (3)Used By (0)

Cart
====

[](#cart)

**DO NOT USE IT ON PRODUCTION BEFORE 1.0.0 IS TAGGED**

[![Travis Build Status](https://camo.githubusercontent.com/edfd276df152feb18eb2e4f7fefeba6d7aa5c6933ae5e846200532290347ce33/68747470733a2f2f7472617669732d63692e6f72672f726f67657276696c612f636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/rogervila/cart)[![Code Climate](https://camo.githubusercontent.com/1fb19268e933139ecbbd0d62e42a1cd0e954fd8ce52c6ac3655680ca75d34bdf/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f726f67657276696c612f636172742f6261646765732f6770612e737667)](https://codeclimate.com/github/rogervila/cart)[![Codeclimate Test Coverage](https://camo.githubusercontent.com/15eb64c5750fd5e796491f330777acc0838472fae0a1ca38bb16b1d15c9cf707/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f726f67657276696c612f636172742f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/rogervila/cart/coverage)[![Codeclimate Issue Count](https://camo.githubusercontent.com/12696c9f365afec4f24187ce4fe20b0731c0a12f56b3dc11bbed68d3f58ec4df/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f726f67657276696c612f636172742f6261646765732f69737375655f636f756e742e737667)](https://codeclimate.com/github/rogervila/cart)[![StyleCI](https://camo.githubusercontent.com/1e892c57cbc5c0f40a98332c8746e000f832d9daf1b4fdca052bca8aa22f62bc/68747470733a2f2f7374796c6563692e696f2f7265706f732f37333238363235302f736869656c64)](https://styleci.io/repos/73286250)[![Appveyor Build status](https://camo.githubusercontent.com/1f169b5aae1e1843f1146ef3c2e22f987ab6cb1b94cdd22e8a5de58ec38604df/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f7873306a7266787430663173337930622f6272616e63682f6d61737465723f7376673d74727565)](https://ci.appveyor.com/project/roger-vila/cart/branch/master)[![Dependency Status](https://camo.githubusercontent.com/78f1e7a8b4d3df6a63196362c9ebd0b7abff19e950bfbdf75a8a9f8f4609af5f/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3538346333356461646630316435303033373462653664352f62616467652e7376673f7374796c653d666c61742d737175617265)](https://www.versioneye.com/user/projects/584c35dadf01d500374be6d5)[![SensioLabsInsight](https://camo.githubusercontent.com/a904197265186941f8932147bd919f5ef7d00ef5ee797c30d4db2ce3e2bb962b/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f62326264343539322d656165642d346435302d626563352d6161633961636465643762342f6269672e706e67)](https://insight.sensiolabs.com/projects/b2bd4592-eaed-4d50-bec5-aac9acded7b4)

Cart is based on sessions, and follows the Fowler's Money pattern.

**Main features**.

- Currency Management
- OOP
- Custom session/cache management
- Framework agnostic
- Easy integration

Install
-------

[](#install)

```
$ composer require rogervila/cart
```

Setup
-----

[](#setup)

Cart has two basic objects: `Cart` and `Item`

### Create a Cart

[](#create-a-cart)

The cart constructor accepts an ID

```
use Cart\Cart;

$cart = new Cart(); // generates an automatic ID
// OR
$cart = new Cart('myCustomId');
```

### Retrieve a Cart

[](#retrieve-a-cart)

If it exists, the Cart will be retrieved from the session by passing it's ID

```
$cart = new Cart('myCustomId'); // If it exists on the session, retrieves it instead of creating a new one
```

### Change the Cart ID

[](#change-the-cart-id)

```
$cart = new Cart(); // generates an automatic ID
$cart->id('myCustomID'); // Changes the ID
```

> When the cart id changes, the old session is not deleted

### Add a currency

[](#add-a-currency)

By default, Cart will work with float numbers if a currency is not set

In order to add a currency, just add this

```
$cart = new Cart();
$cart->currency('EUR'); // add an ISO4217 currency
```

### Create Items

[](#create-items)

When an Item is created, it **must** receive a unique ID

```
use Cart\Item;

$item = new Item('mandatoryUniqueId');
```

### Add Item data

[](#add-item-data)

Instead of passing only the ID, an array with data can be passed.

```
$item = new Item([
    'id' => 'uniqueId',
    'name' => 'Banana',
    'quantity' => 1, // must be an integer
    'price' => '0.99' // it accepts strings and integers
]);
```

### Add Item custom data

[](#add-item-custom-data)

In order to add custom fields, a `fields()` method is provided

```
$fields = [
    'foo' => 'bar'
]

$item->fields($fields);
```

> When the item price is set with an integer, it will be parsed as cents, so `(int) 999` will be parsed as `(string) '9.99'`

Also, Item data can be added with fluent

```
    $item = new Item(123);
    $item->quantity(2)->price('0.99')->name('Banana');
```

### Add Items to the cart

[](#add-items-to-the-cart)

If the item does not have a quantity, it will be set to 1

```
$items = [
    new Item('id1'),
    new Item('id2'),
]

$cart->add($items);

// OR

$cart->add($item1)->add($item2); // etc...
```

### Get subtotal

[](#get-subtotal)

Gets the sum from all Cart Items

```
var_dump($cart->subtotal());
```

### Add Fees

[](#add-fees)

Fees can have a percentage or a fixed value

TODO

### Add Conditions

[](#add-conditions)

TODO

### Get total

[](#get-total)

Gets the final result, after applying Item conditions, Cart conditions and Fees

TODO

Todos
-----

[](#todos)

- Full documentation
- Allow price conversion when the currency changes
- Choose between automatic and manual conversion
- Update the cart items currency when the Cart currency is changed
- Integrate Conditions (discounts, coupons, etc) with custom rules
- Add Fees (Taxes, Shipping, etc)
- More tests

License
-------

[](#license)

MIT

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity44

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/351443b7d23e94fcf31b250db90f0b9578cc9fd8e0cefbed9666467e3e9cb571?d=identicon)[rogervila](/maintainers/rogervila)

---

Top Contributors

[![rogervila](https://avatars.githubusercontent.com/u/6053012?v=4)](https://github.com/rogervila "rogervila (94 commits)")

---

Tags

cartcurrencyecommercefowler-money-patternlibrary

### Embed Badge

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

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

###  Alternatives

[eftec/autoloadone

AutoloadOne is a program that generates an autoload class for PHP.

403.4k](/packages/eftec-autoloadone)

PHPackages © 2026

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