PHP Classes

PHP Cart Page: Manage items in a shopping cart

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 63%Total: 357 All time: 6,939 This week: 47Up
Version License PHP version Categories
riesenia-cart 1.0.0MIT/X Consortium ...5.4PHP 5, E-Commerce
Description 

Author

This package can manage items in a shopping cart.

It can perform several types of operations that manipulate the items in a shopping cart. Currently it can:

- Add or remove items
- Check if a item was added and retrieve its details
- Set an item quantity
- Check if the cart is empty or has no items of a given type
- Get all items by type or with an optional callback filter
- Get an item price
- Get the cart totals or sub-totals by price

Setting a context value, adding VAT taxes and setting the number of price decimal cases are configurable options

Picture of Tomas Saghy
  Performance   Level  
Name: Tomas Saghy <contact>
Classes: 2 packages by
Country: Slovak Republic Slovak Republic
Age: ???
All time rank: 351618 in Slovak Republic Slovak Republic
Week rank: 195 Up2 in Slovak Republic Slovak Republic Up

Documentation

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

  Files folder image Files (9)  
File Role Description
Files folder imagespec (1 file)
Files folder imagesrc (3 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpspec.yml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (9)  /  spec  
File Role Description
  Plain text file CartSpec.php Class Class source

  Files folder image Files (9)  /  src  
File Role Description
  Plain text file Cart.php Class Class source
  Plain text file CartItemInterface.php Class Class source
  Plain text file WeightedCartItemInterface.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:357
This week:0
All time:6,939
This week:47Up
User Ratings User Comments (1)
 All time
Utility:87%StarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:63%StarStarStarStar
Rank:850