riesenia/cart

PHP library providing shopping cart functionality

v4.1.1 2022-07-01 15:36 UTC

README

Build Status Latest Version Total Downloads Software License

PHP library providing basic shopping cart functionality.

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

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 ([], true, 2)
$cart = new Cart();

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

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

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. It accepts callable or string (see examples for getTotal) to filter results.

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

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

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

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.

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

You can run the unit tests with the following command:

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