riesenia / cart
PHP library providing shopping cart functionality
Installs: 28 908
Dependents: 0
Suggesters: 0
Security: 0
Stars: 48
Watchers: 9
Forks: 9
Open Issues: 0
Requires
- php: ^7.1 || ^8.0
- litipk/php-bignumbers: ~0.8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpspec/phpspec: ^5.0 || ^6.0 || ^7.0
- rshop/php-cs-fixer-config: ^2.0
- dev-master
- v4.1.1
- v4.1.0
- v4.0.0
- v3.0.1
- v3.0.0
- 2.2.x-dev
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.9.1
- v1.9.0
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.1
- v1.0.0
- dev-fix-context-reset
- dev-add-stickler-config
This package is auto-updated.
Last update: 2024-10-29 04:31:02 UTC
README
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