PHP library providing shopping cart functionality

v3.0.1 2019-04-25 14:41 UTC


Build Status Latest Version Total Downloads Software License

PHP library providing basic shopping cart functionality.


Install the latest version using composer require riesenia/cart

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

    "require": {
        "riesenia/cart": "~3.0"

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


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)
  • number of decimals for rounding

All of them can be set separately.

use Riesenia\Cart\Cart;

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

$cart->setContext(['customer_id' => $_SESSION['customer_id']]);

Manipulating cart items

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

// adding item to cart ($product has to implement CartItemInterface)

// 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

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

Items can be fetched using getItems (accepts callable to filter results) or by type using getItemsByType.

Counting totals

Cart works with Decimal class (see litipk/php-bignumbers). 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 count 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

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

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');

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

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


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.


You can run the unit tests with the following command:

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