PHPackages                             treehousetim/shopcart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. treehousetim/shopcart

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

treehousetim/shopcart
=====================

A simple, zero-dependency, zero UI, Shopping Cart library.

v0.0.8(4y ago)1806↓50%3MITPHPPHP ^7.0CI failing

Since Apr 30Pushed 4y ago3 watchersCompare

[ Source](https://github.com/treehousetim/shopCart)[ Packagist](https://packagist.org/packages/treehousetim/shopcart)[ RSS](/packages/treehousetim-shopcart/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (9)Used By (0)

shopCart
========

[](#shopcart)

Shopping Cart Core Functionality PHP Library

Installing
----------

[](#installing)

`composer require treehousetim/shopcart`

Interfaces and Abstract Classes
-------------------------------

[](#interfaces-and-abstract-classes)

In order to use the shopCart, you will need to instantiate both a `treehousetim\shopCart\cart` and a `treehousetim\shopCart\catalog` object.

The constructor for the cart requires a catalog object be passed. In order for a catalog object to function, it requires a product loader be implemented that implements `catalogLoaderInterface`

```
use \treehousetim\shopCart\catalog;
use \treehousetim\shopCart\cart;

$catalog = ( new catalog() )
	->setProductLoader( new \application\cart\myProductLoader() )
	->populate();

$cart = new cart( $catalog );
$cart->setTotalTypeLoader( (new \application\cart\myCatalogTotalTypeLoader()))
	->setFormatter( new \application\cart\myProductAmountFormatter() )
	->setStorageHandler( (new \treehousetim\shopCart\cartStorageSession() ) )
	->load();
```

catalogLoaderInterface
----------------------

[](#catalogloaderinterface)

A catalog object is used to load products into the product catalog.

```
interface catalogLoaderInterface
{
	public function hasProducts() : bool;
	public function nextProduct() : bool;
	public function getProduct() : product;
}
```

cartStorageInterface
--------------------

[](#cartstorageinterface)

You can use a lot of different storage options for a cart. `treehousetim\shopCart` provides an implementation for session storage.

After creating your implementation of this interface, you must supply it to your cart using `$cart->setStorageHandler( $storageHandler );`

```
interface cartStorageInterface
{
	public function loadCart( cart $cart ) : cartStorageInterface;
	public function emptyCart( cart $cart ) : cartStorageInterface;

	public function saveItems( array $items ) : cartStorageInterface;
	public function saveData( array $data ) : cartStorageInterface;

	// used to clean up after all storage is complete
	public function finalize( cart $cart ) : cartStorageInterface;
}
```

catalogTotalTypeLoaderInterface
-------------------------------

[](#catalogtotaltypeloaderinterface)

You must implement a class that conforms to this interface to support different types of product totals. Typical e-commerce shop carts will probably only need a single catalogTotalType for price, but there are other businesses that need totals of different product properties.

```
interface catalogTotalTypeLoaderInterface
{
	public function nextType() : bool;
	public function getType() : catalogTotalType;
	public function resetType();
}
```

Here is a sample catalogTotalTypeLoaderInterface implementation for both price and some special case of "points"

```
