PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Tomas Saghy   PHP Cart Page   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Cart Page
Manage items in a shopping cart
Author: By
Last change:
Date: 8 years ago
Size: 3,377 bytes
 

Contents

Class file image Download

PHP Cart

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": "~1.0"
    }
}

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 (null, 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 class has to implement CartItemInterface)
$cart->addItem($product);

// set quantity of the item
$cart->addItem($anotherProduct, 3);

// when $product->getCartId() returns i.e. 'abc'
$cart->setItemQuantity('abc', 7);

// removing 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. All items have to implement CartItemInterface.

Getting items

Items can be fetched using getItems or by type using getItemsByType.

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

Getting weight (since 1.4)

As of 1.4 item implementing WeightedCartItemInterface can be added to cart, so cart can count total weight. Weight can be counted by type using same format as for counting totals.

// get weight of type 'product'
echo $cart->getWeight('product');

Tests

You can run the unit tests with the following command:

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