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

ActiveLibrary

riesenia/cart
=============

PHP library providing shopping cart functionality

v4.1.1(3y ago)4838.0k↓27.3%8MITPHPPHP ^7.1 || ^8.0

Since Jun 18Pushed 3y ago9 watchersCompare

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

READMEChangelog (10)Dependencies (4)Versions (45)Used By (0)

PHP Cart
========

[](#php-cart)

[![Build Status](https://github.com/riesenia/cart/workflows/Test/badge.svg)](https://github.com/riesenia/cart/actions)[![Latest Version](https://camo.githubusercontent.com/02896b377dd4bdfaf9a87f0ba1c6f0bf42d929c79ec47b2c8f0792b09c14abd0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72696573656e69612f636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/riesenia/cart)[![Total Downloads](https://camo.githubusercontent.com/e916db049feccd0b4c8f36ac3ec21374208fb111826896d757271e9f3ec85913/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72696573656e69612f636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/riesenia/cart)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

PHP library providing basic shopping cart functionality.

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

[](#installation)

Install the latest version using `composer require riesenia/cart`

Or add to your *composer.json* file as a requirement:

```
{
    "require": {
        "riesenia/cart": "~4.0"
    }
}
```

*Note: if you use PHP 5.4 - 5.6 use 1.\* version of this library.*

Usage
-----

[](#usage)

Constructor takes three configuration parameters:

- context data that are passed to each added cart item (you can pass i.e. customer id to resolve custom price)
- true when listing gross prices, false for net prices (see [nice explanation](http://makandracards.com/makandra/1505-invoices-how-to-properly-round-and-calculate-totals))
- number of decimals for rounding

All of them can be set separately.

```
use Riesenia\Cart\Cart;

// default is ([], true, 2)
$cart = new Cart();

$cart->setContext(['customer_id' => $_SESSION['customer_id']]);
$cart->setPricesWithVat(false);
$cart->setRoundingDecimals(4);
```

Manipulating cart items
-----------------------

[](#manipulating-cart-items)

Items can be accessed by their cart id (provided by *getCartId* method).

```
// adding item to cart ($product has to implement CartItemInterface)
$cart->addItem($product);

// set quantity of the item when adding to cart
$cart->addItem($anotherProduct, 3);

// when $product->getCartId() returns i.e. 'abc'
$cart->setItemQuantity('abc', 7);

// remove item
$cart->removeItem('abc');
```

### Batch cart items manipulation

[](#batch-cart-items-manipulation)

Cart can be cleared using *clear()* method. Items can be set using *setItems()* method. Please note that *setItems* will call *clear* first. All added items have to implement *CartItemInterface*.

### Getting items

[](#getting-items)

Items can be fetched using *getItems*. It accepts *callable* or string (see examples for `getTotal`) to filter results.

Counting totals
---------------

[](#counting-totals)

Cart works with *Decimal* class (see [litipk/php-bignumbers](https://github.com/Litipk/php-bignumbers/wiki/Decimal)). You can access subtotal (without VAT), taxes (array of amounts for all rates) and total (subtotal + taxes).

```
// item 1 [price: 1.00, tax rate: 10]
// item 2 [price: 2.00, tax rate: 20]

// 3.00
echo $cart->getSubtotal();

// 0.10
echo $cart->getTaxes()[10];

// 0.40
echo $cart->getTaxes()[20];

// 3.50
echo $cart->getTotal();
```

Totals can be also counted by type:

```
// get totals of type 'product'
echo $cart->getTotal('product');

// get totals of type 'product' and 'service'
echo $cart->getTotal('product,service');

// get totals of all items except type 'product' and 'service'
echo $cart->getTotal('~product,service');
```

### Counting item price

[](#counting-item-price)

You can get price of an item using *getItemPrice* method. It sets the cart context before counting the price, but you can modify params to get i.e. price without VAT.

```
$cart = new Cart();
$cart->addItem($product, 3);

// get unit price without VAT
echo $cart->getItemPrice($product, 1, false);
```

### Getting cart weight

[](#getting-cart-weight)

Item implementing *WeightedCartItemInterface* can be added to cart, so cart can count total weight. Weight can be counted by type using the same format as for counting totals.

```
// get weight of type 'product'
echo $cart->getWeight('product');
```

Total rounding
--------------

[](#total-rounding)

Rounding function can be set using *setTotalRounding* method. This affects only total sum of the cart. Rounding amount can be accessed using *getRoundingAmount* method.

Bound cart items
----------------

[](#bound-cart-items)

Item implementing *BoundCartItemInterface* can be added to cart. When the target item is removed from the cart, bound item is removed automatically too. If *updateCartQuantityAutomatically* method returns true, bound item also reflects quantity changes of target item.

### Multiple bound cart items

[](#multiple-bound-cart-items)

Item implementing *MultipleBoundCartItemInterface* can be added to cart. When any of target items is removed from the cart, bound item is removed automatically too.

Promotions
----------

[](#promotions)

You can set promotions using *setPromotions* method. Each promotion has to implement *PromotionInterface*. Please note that *beforeApply* and *afterApply* callbacks will be called always even if promotion is not eligible. Method *apply* will be called only if promotion passes *isEligible* test.

Tests
-----

[](#tests)

You can run the unit tests with the following command:

```
cd path/to/riesenia/cart
composer install
vendor/bin/phpspec run
```

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 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 ~62 days

Recently: every ~15 days

Total

42

Last Release

1417d ago

Major Versions

v1.9.1 → v2.0.02017-10-23

v2.1.1 → v3.0.02018-08-03

v2.1.2 → v4.0.02022-05-04

PHP version history (4 changes)v1.0.0PHP &gt;=5.4

v2.0.0PHP &gt;=7.0

v3.0.0PHP &gt;=7.1

v4.0.0PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/40c7ed7cfaebeddae57ac4a376c8f21df56dd2f38821b7ba92ea1312ef8020c8?d=identicon)[riesenia](/maintainers/riesenia)

---

Top Contributors

[![segy](https://avatars.githubusercontent.com/u/1355459?v=4)](https://github.com/segy "segy (55 commits)")

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[shopsys/framework

Core of Shopsys Platform - open source framework for building large, scalable, fast-growing e-commerce projects based on Symfony

25211.4k19](/packages/shopsys-framework)[soliantconsulting/simplefm

FileMaker Server XML API Adapter

556.2k4](/packages/soliantconsulting-simplefm)

PHPackages © 2026

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