Download the PHP package riesenia/cart without Composer
On this page you can find all versions of the php package riesenia/cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download riesenia/cart
More information about riesenia/cart
Files in riesenia/cart
Package cart
Short Description PHP library providing shopping cart functionality
License MIT
Informations about the package cart
PHP Cart
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:
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.
Manipulating cart items
Items can be accessed by their cart id (provided by getCartId method).
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).
Totals can be also counted by type:
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.
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.
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: