PHPackages                             karelbartunek/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. [Payment Processing](/categories/payments)
4. /
5. karelbartunek/cart

ActiveLibrary[Payment Processing](/categories/payments)

karelbartunek/cart
==================

A flexible and modern shopping cart package

v3.0.2(6y ago)0112MITPHPPHP &gt;=5.6.0

Since Nov 11Pushed 6y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (12)Used By (0)

Cart
====

[](#cart)

[![Packagist](https://camo.githubusercontent.com/cbf96f7873c2b70a58282c21e135b81b06b848a93fe2777632d10f4e4e152814/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d696b65313832756b2f636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mike182uk/cart)[![Build Status](https://camo.githubusercontent.com/660ca8c08471464550ab4428d7854ce623c8c87f86d3178558112a83b905e4b8/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d696b65313832756b2f636172742e7376673f7374796c653d666c61742d737175617265)](http://travis-ci.org/mike182uk/cart)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/172e378cb0992c2760c1175c1c835f624d265878953f5bdc3dd83d10db6ad4c4/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d696b65313832756b2f636172742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mike182uk/cart/)[![SensioLabs Insight](https://camo.githubusercontent.com/dff2beb962483124219cb3de0f080e9417ff55c9923ffd5d0ba94e902de55dfd/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f31643832303438612d313339302d343264352d383630352d3630363534316538316339382e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/1d82048a-1390-42d5-8605-606541e81c98)[![Code Coverage](https://camo.githubusercontent.com/8843815f1d6dbb3b8e5738625d015130fcc2621497ab54bb2beccc6075621f9f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d696b65313832756b2f636172742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mike182uk/cart/)[![Total Downloads](https://camo.githubusercontent.com/620982142e8b77388f93ebb71c623b1da7d49c2619606dfface71eb73f77a5c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d696b65313832756b2f636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mike182uk/cart)[![License](https://camo.githubusercontent.com/bd42c2fb171704022568a65d9888c8baf1ddd067bb7bd86c42c4d0a585da3472/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d696b65313832756b2f636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mike182uk/cart)

A flexible and modern shopping cart package.

Prerequisites
-------------

[](#prerequisites)

- PHP &gt;=5.6.0

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

[](#installation)

```
composer require mike182uk/cart
```

Usage
-----

[](#usage)

- [Cart](#cart)
- [Cart Item](#cart-item)
- [Cart Storage Implementation](#cart-store)

### Cart

[](#cart-1)

#### Create a new cart

[](#create-a-new-cart)

To create a new cart instance you must pass an id and a storage implementation to the cart constructor:

```
use Cart\Cart;
use Cart\Storage\SessionStore;

$id = 'cart-01';
$cartSessionStore = new SessionStore();

$cart = new Cart($id, $cartSessionStore);
```

The storage implementation must implement `Cart\Storage\Store`.

The id is used for saving / restoring cart state via the storage implementation.

#### Add an item to the cart

[](#add-an-item-to-the-cart)

Use the `add` method to add an item to the cart. A valid `Cart\CartItem` must be passed to this method.

```
use Cart\CartItem;

$item = new CartItem;
$item->name = 'Macbook Pro';
$item->sku = 'MBP8GB';
$item->price = 1200;
$item->tax = 200;

$cart->add($item);
```

If the item already exists in the cart, the quantity of the existing item will be updated to include the quantity of the item being added.

#### Remove an item from the cart

[](#remove-an-item-from-the-cart)

Remove an item from the cart by passing the item id to the `remove` method.

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

#### Update an item in the cart

[](#update-an-item-in-the-cart)

To update a property of an item in the cart use the `update` method. You will need to pass the cart item id, the name of the property to update and the new value. This method will return the item id (in case it has changed due to the update).

```
$newId = $cart->update('e4df90d236966195b49b0f01f5ce360a356bc76b', 'price', 959.99);
```

If you try and update an item that does not exist in the cart a `InvalidArgumentException` will be thrown.

#### Retrieve an item in the cart

[](#retrieve-an-item-in-the-cart)

Retrieve an item from the cart by its id use the `get` method. If the item does not exist `null` is returned.

```
$item = $cart->get('e4df90d236966195b49b0f01f5ce360a356bc76b');

if ($item) {
    // ...
}
```

#### Retrieve all items in the cart

[](#retrieve-all-items-in-the-cart)

Retrieve all items in the cart using the `all` method. This will return an array of all the items in the cart.

```
$cartItems = $cart->all();

if (count($cartItems) > 0) {
    foreach ($cartItems as $item) {
        // ...
    }
}
```

#### Determine if an item exists in the cart

[](#determine-if-an-item-exists-in-the-cart)

Determine if an item exists in the cart using the `has` method. Returns `true` or `false`.

```
if ($cart->has('e4df90d236966195b49b0f01f5ce360a356bc76b')) {
    // ...
}
```

#### Clear The Cart

[](#clear-the-cart)

Clear the cart using the `clear` method.

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

This will also clear the saved state for this cart in the store.

#### Save / restore cart state

[](#save--restore-cart-state)

Save the cart using the `save` method.

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

This will save the current cart items and cart id to the store.

Restore the cart using the `restore` method.

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

This will add any stored cart items back to the cart and set the cart id. If there is a problem restoring the cart a `Cart\CartRestoreException` will be thrown. This will only happen if:

- the saved data is unserializable
- the unserialized data is invalid (not an array)
- the cart id is not present in the unserialized data
- the cart items are not present in the unserialized data
- the cart id is invalid (not a string)
- the cart items are invalid (not an array)

#### Other Cart Methods

[](#other-cart-methods)

##### totalUniqueItems

[](#totaluniqueitems)

Get the total number of unique items in the cart.

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

##### totalItems

[](#totalitems)

Get the total number of items in the cart.

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

##### total

[](#total)

Get the total price of all the cart items including tax.

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

You can get the total excluding tax by using the `totalExcludingTax` method.

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

##### tax

[](#tax)

Get the total tax of all the cart items.

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

##### toArray

[](#toarray)

Export the cart to an array.

```
$cartData = $cart->toArray();
```

Array will be structured like:

```
[
    'id' => 'cart-01', // cart id
    'items' => [
        // cart items as array
    ]
]
```

##### getId

[](#getid)

Get the id of the cart.

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

##### getStore

[](#getstore)

Get the cart storage implementation.

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

### Cart Item

[](#cart-item)

#### Create a new Cart Item

[](#create-a-new-cart-item)

```
use Cart\CartItem;

$item = new CartItem;

$item->name = 'Macbook Pro';
$item->sku = 'MBP8GB';
$item->price = 1200;
$item->tax = 200;
$item->options = [
    'ram' => '8 GB',
    'ssd' => '256 GB'
];
```

`Cart\CartItem` implements `ArrayAccess` so properties can be assigned to the cart item as if accessing an array:

```
$item = new CartItem;

$item['name'] = 'Macbook Pro';
$item['sku'] = 'MBP8GB';
$item['price'] = 1200;
$item['tax'] = 200;
$item['options'] = [
    'ram' => '8 GB',
    'ssd' => '256 GB'
];
```

An array of data can also be passed to the cart item constructor to set the cart item properties:

```
$itemData = [
    'name' => 'Macbook Pro';
    'sku' => 'MBP8GB';
    'price' => 1200;
    'tax' => 200;
    'options' => [
        'ram' => '8 GB',
        'ssd' => '256 GB'
    ]
];

$item = new CartItem($itemData);
```

If no quantity is passed to the cart item constructor, the quantity is set to `1` by default.

If no price is passed to the cart item constructor, the price is set to `0.00` by default.

If no tax is passed to the cart item constructor, the tax is set to `0.00` by default.

#### Cart Item ID

[](#cart-item-id)

Each cart has a unique ID. This ID is generated using the properties set on the cart item. You can get the cart item ID using the method `getId` or by accessing the property `id`.

```
$id = $item->getId();
```

```
$id = $item->id;
```

```
$id = $item['id'];
```

**Changing a property on the cart item will change its ID.**

#### Cart Item Methods

[](#cart-item-methods)

#### get

[](#get)

Get a piece of data set on the cart item.

```
$name = $item->get('name');
```

This is the same as doing:

```
$name = $item['name'];
```

```
$name = $item->name;
```

#### set

[](#set)

Set a piece of data on the cart item.

```
$item->set('name', 'Macbook Pro');
```

This is the same as doing:

```
$item['name'] = 'Macbook Pro';
```

```
$item->name = 'Macbook Pro';
```

If you are setting the item quantity, the value must be an integer otherwise an `InvalidArgumentException` is thrown.

```
$item->quantity = 1; // ok

$item->quantity = '1' // will throw exception
```

If you are setting the item price or tax, the value must be numeric otherwise an `InvalidArgumentException` is thrown.

```
$item->price = 10.00; // ok

$item->price = '10' // ok

$item->price = 'ten' // will throw exception
```

##### getTotalPrice

[](#gettotalprice)

Get the total price of the cart item including tax `((item price + item tax) * quantity)`.

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

You can also get the total price excluding tax `(item price * quantity)` using the `getTotalPriceExcludingTax` method.

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

##### getSinglePrice

[](#getsingleprice)

Get the single price of the cart item including tax `(item price + item tax)`

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

You can also get the single price excluding tax by using the `getSinglePriceExcludingTax` method.

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

##### getTotalTax

[](#gettotaltax)

Get the total tax of the cart item `(item tax * quantity)`.

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

##### getSingleTax

[](#getsingletax)

Get the single tax value of the cart item.

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

##### toArray

[](#toarray-1)

Export the item to an array.

```
$itemArr = $item->toArray();
```

Array will be structured like:

```
[
    'id' => 'e4df90d236966195b49b0f01f5ce360a356bc76b', // cart item unique id
    'data' => [
        'name' => 'Macbook Pro',
        'sku' => 'MBP8GB',
        'price' => 1200,

        // ... other cart item properties
    ]
]
```

### Cart Storage Implementation

[](#cart-storage-implementation)

A cart storage implementation must implement `Cart\Storage\Store`.

This package provides 2 basic storage implementations: `Cart\Storage\SessionStore` and `Cart\Storage\CookieStore`.

When the `save` method of the cart is called, the cart id and serialized cart data is passed to the `put` method of the storage implementation.

When the `restore` method of the cart is called, the cart id is passed to the `get` method of the storage implementation.

When the `clear` method of the cart is called, the cart id is passed to the `flush` method of the storage implementation.

An example session storage implementation may look like:

```
use Cart\Store;

class SessionStore implements Store
{
    /**
     * {@inheritdoc}
     */
    public function get($cartId)
    {
        return isset($_SESSION[$cartId]) ? $_SESSION[$cartId] : serialize([]);
    }

    /**
     * {@inheritdoc}
     */
    public function put($cartId, $data)
    {
        $_SESSION[$cartId] = $data;
    }

    /**
     * {@inheritdoc}
     */
    public function flush($cartId)
    {
        unset($_SESSION[$cartId]);
    }
}
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 96.8% 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 ~248 days

Recently: every ~396 days

Total

11

Last Release

2444d ago

Major Versions

v1.1.0 → v2.0.02013-11-02

v2.2.3 → v3.0.02017-01-04

PHP version history (2 changes)v1.0.0PHP &gt;=5.3.0

v3.0.0PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f62424ca41e51e2509dad518de98e983f5cceab448cc892b5d0e349e121c4fb?d=identicon)[voucherino](/maintainers/voucherino)

---

Top Contributors

[![mike182uk](https://avatars.githubusercontent.com/u/991592?v=4)](https://github.com/mike182uk "mike182uk (122 commits)")[![karelbartunek](https://avatars.githubusercontent.com/u/1294842?v=4)](https://github.com/karelbartunek "karelbartunek (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[omnipay/paypal

PayPal gateway for Omnipay payment processing library

3156.8M53](/packages/omnipay-paypal)[eduardokum/laravel-boleto

Biblioteca com boletos para o laravel

626351.9k2](/packages/eduardokum-laravel-boleto)[tbbc/money-bundle

This is a Symfony bundle that integrates moneyphp/money library (Fowler pattern): https://github.com/moneyphp/money.

1961.9M](/packages/tbbc-money-bundle)[2checkout/2checkout-php

2Checkout PHP Library

83740.3k2](/packages/2checkout-2checkout-php)[smhg/sepa-qr-data

Generate QR code data for SEPA payments

61717.2k5](/packages/smhg-sepa-qr-data)[omnipay/dummy

Dummy driver for the Omnipay payment processing library

271.2M33](/packages/omnipay-dummy)

PHPackages © 2026

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