2017 © Pedro Peláez
 

library cart

A framework agnostic shopping cart package.

image

jamesdb/cart

A framework agnostic shopping cart package.

  • Saturday, November 18, 2017
  • by jamesdb
  • Repository
  • 3 Watchers
  • 10 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Shopping Cart

Software License Build Status Coverage Status Quality Score Total Downloads, (*1)

A framework agnostic shopping cart package., (*2)

Install

Via Composer, (*3)

$ composer require jamesdb/cart

Notes

Money Value Objects

This package uses moneyphp/money (an implementation of Martin Fowler's money pattern). Floats are avoided due to them being ill-suited for monetary values. More information can be found in the links below:, (*4)

Due to this when dealing with monetary values you will need to represent them as integers instead of floats., (*5)

An example of this can be found below., (*6)

use jamesdb\Cart\CartItem;

$item = new CartItem([
    ...
    'price' => 1099, // Instead of £10.99 or $10.99 etc.
    ...
]);

Setting Up

To setup a cart instance you need to pass an identifier and storage implementation to the cart constructor., (*7)

The currency defaults to 'GBP', if you want to change this you will need to pass your currency of choice into the setCurrency method as shown below., (*8)

A custom formatter callback can be setup via setFormatterCallback, see moneyphp formatters for more information., (*9)

use jamesdb\Cart\Cart;
use jamesdb\Cart\Storage\NativeSessionDriver;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\DecimalMoneyFormatter;

$cart = new Cart('cart', new NativeSessionDriver);
$cart->setCurrency(new Currency('GBP'));

$cart->setFormatterCallback(function ($money) {
    $currencies = new ISOCurrencies();
    $moneyFormatter = new DecimalMoneyFormatter($currencies);

    return $moneyFormatter->format($money); // outputs in decimal format.
});

Any storage implementation can be used as long as it implements the jamesdb\Cart\Storage\StorageInterface., (*10)

Usage

Adding Items

CartItem implements ArrayAccess and uses the __set and __get magic methods to assign and access properties., (*11)

Added items will return a rowid., (*12)

use jamesdb\Cart\Cart;
use jamesdb\Cart\CartItem;

$cart = new Cart(...);

/**
 * Assign properties via __set.
 */
$item1 = new CartItem();
$item1->id = 2731;
$item1->name = 'Product';
$item1->price = 1099;
$item1->quantity = 1;

$cart->add($item1);

/**
 * ----------
 */

/**
 * Assign properties via ArrayAccess.
 */
$item2 = new CartItem([
    'id' => 2731,
    'name' => 'Product',
    'price' => 1099,
    'quantity' => 1
]);

$cart->add($item2)

Updating Items

You can either use the cart update method or access the properties directly on the CartItem., (*13)

Attempting to update an item that doesn't exist in the cart will result in a CartItemUpdateException being thrown., (*14)

$cart->update('rowid', ['name' => 'Renamed Product']);

/**
 * ----------
 */

$cartItem = $cart->find('rowid');
$cartItem->name = 'Renamed Product';

Removing Items

Attempting to remove an item that doesn't exist in the cart will result in a CartItemRemoveException being thrown., (*15)

$cart->remove('rowid');

Clear the Cart

$cart->clear();

Accessing a specific Item

To access a specific item use the cart getItem method., (*16)

If an item can't be found the method will return null., (*17)

$cart->getItem('rowid');

Accessing all Items

$cart->getItems();

Filtering Items

The cart can be filtered by any supplied key and value with the filter method., (*18)

// Return all items with a quantity of 2.
$cart->filter('quantity', 2);

// Return all items with a price of 1000.
$cart->filter('price', 1000);

Accessing Item counts

The getTotalUniqueItems method will return an item count excluding quantities., (*19)

$cart->getTotalUniqueItems();

The getTotalItems method will return an item count including quantities., (*20)

$cart->getTotalItems();

A convenience method isEmpty is built into the cart, this proxies through to the getTotalUniqueItems with a === 0 check., (*21)

$cart->isEmpty();

Accessing prices

Get the overall price including tax with the getTotalPrice method., (*22)

$cart->getTotalPrice();

Occasionally dealing with tax isn't required, in this case you can use the getTotalPriceExcludingTax method., (*23)

$cart->getTotalPriceExcludingTax();

Get the total cart tax., (*24)

$cart->getTax();

Events

League\Event is built into the cart and provides a number of events that can be emitted, this allows you to easily hook into certain key points during the carts lifecycle., (*25)

You can subscribe to these events by attaching listeners to the cart via addEventListener., (*26)

All events have built in methods to retrieve the item triggering the event and the cart itself., (*27)

$cart->addEventListener('cart.add', function ($event) {
    $item = $event->getItem();
    $cart = $event->getCart();

    ...
});

cart.add

$cart->addEventListener('cart.add', function ($event) {});

cart.update

$cart->addEventListener('cart.update', function ($event) {});

cart.remove

$cart->addEventListener('cart.remove', function ($event) {});

The Versions

18/11 2017

dev-master

9999999-dev http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

26/11 2016

dev-dev

dev-dev http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

20/11 2016

0.3.0

0.3.0.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

20/03 2016

0.2.1

0.2.1.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

20/03 2016

0.2.0

0.2.0.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

07/01 2016

0.1.13

0.1.13.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

03/01 2016

0.1.12

0.1.12.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

03/01 2016

0.1.11

0.1.11.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

03/01 2016

0.1.10

0.1.10.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

03/11 2015

0.1.9

0.1.9.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

02/11 2015

0.1.8

0.1.8.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

02/11 2015

0.1.7

0.1.7.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

02/11 2015

0.1.6

0.1.6.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

02/11 2015

0.1.5

0.1.5.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

02/11 2015

0.1.4

0.1.4.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

02/11 2015

0.1.3

0.1.3.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

29/09 2015

0.1.2

0.1.2.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

29/09 2015

0.1.1

0.1.1.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping

28/09 2015

0.1.0

0.1.0.0 http://github.com/jamesdb/cart

A framework agnostic shopping cart package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Baxter

cart shopping