2017 © Pedro Peláez
 

library cart

PHP library providing shopping cart functionality

image

riesenia/cart

PHP library providing shopping cart functionality

  • Wednesday, August 1, 2018
  • by riesenia
  • Repository
  • 3 Watchers
  • 21 Stars
  • 1,543 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 2 Open issues
  • 36 Versions
  • 5 % Grown

The README.md

PHP Cart

Build Status Latest Version Total Downloads Software License, (*1)

PHP library providing basic shopping cart functionality., (*2)

Installation

Install the latest version using composer require riesenia/cart, (*3)

Or add to your composer.json file as a requirement:, (*4)

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

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

Usage

Constructor takes three configuration parameters:, (*6)

  • 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., (*7)

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)., (*8)

// 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., (*9)

Getting items

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

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)., (*11)

// 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:, (*12)

// 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., (*13)

$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., (*14)

// 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., (*15)

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., (*16)

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., (*17)

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., (*18)

Tests

You can run the unit tests with the following command:, (*19)

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

The Versions

01/08 2018

dev-master

9999999-dev

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

21/03 2018

v2.1.1

2.1.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

21/03 2018

v2.1.0

2.1.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

19/01 2018

dev-add-stickler-config

dev-add-stickler-config

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

12/12 2017

v2.0.1

2.0.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

23/10 2017

v2.0.0

2.0.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

03/07 2017

v1.9.1

1.9.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

13/04 2017

v1.9.0

1.9.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

16/01 2017

v1.8.5

1.8.5.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

24/11 2016

v1.8.4

1.8.4.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

22/08 2016

v1.8.3

1.8.3.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

21/08 2016

v1.8.2

1.8.2.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

21/08 2016

v1.8.1

1.8.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

19/08 2016

v1.8.0

1.8.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

18/08 2016

v1.7.2

1.7.2.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

18/08 2016

v1.7.1

1.7.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

24/06 2016

v1.7.0

1.7.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

24/06 2016

v1.6.1

1.6.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

23/06 2016

v1.6.0

1.6.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

25/05 2016

v1.5.3

1.5.3.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

06/04 2016

v1.5.2

1.5.2.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

04/04 2016

v1.5.1

1.5.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

02/04 2016

v1.5.0

1.5.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

15/02 2016

v1.4.3

1.4.3.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

15/02 2016

v1.4.2

1.4.2.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

21/01 2016

v1.4.1

1.4.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

20/01 2016

v1.4.0

1.4.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

20/01 2016

v1.3.4

1.3.4.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

11/01 2016

v1.3.3

1.3.3.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

11/01 2016

v1.3.2

1.3.2.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

11/01 2016

v1.3.1

1.3.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

16/07 2015

v1.3.0

1.3.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

15/07 2015

v1.2.0

1.2.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

26/06 2015

v1.1.0

1.1.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

26/06 2015

v1.0.1

1.0.1.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy

18/06 2015

v1.0.0

1.0.0.0

PHP library providing shopping cart functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tomas Saghy