Download the PHP package jamesdb/cart without Composer
On this page you can find all versions of the php package jamesdb/cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package cart
Short Description A framework agnostic shopping cart package.
License MIT
Homepage http://github.com/jamesdb/cart
Informations about the package cart
Shopping Cart
A framework agnostic shopping cart package.
Install
Via Composer
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:
- http://martinfowler.com/eaaCatalog/money.html
- http://culttt.com/2014/05/28/handle-money-currency-web-applications/
Due to this when dealing with monetary values you will need to represent them as integers instead of floats.
An example of this can be found below.
Setting Up
To setup a cart instance you need to pass an identifier and storage implementation to the cart constructor.
The currency defaults to 'GBP', if you want to change this you will need to pass your currency of choice into the method as shown below.
A custom formatter callback can be setup via , see moneyphp formatters for more information.
Any storage implementation can be used as long as it implements the .
Usage
Adding Items
implements and uses the and magic methods to assign and access properties.
Added items will return a rowid.
Updating Items
You can either use the cart method or access the properties directly on the .
Attempting to update an item that doesn't exist in the cart will result in a being thrown.
Removing Items
Attempting to remove an item that doesn't exist in the cart will result in a being thrown.
Clear the Cart
Accessing a specific Item
To access a specific item use the cart method.
If an item can't be found the method will return .
Accessing all Items
Filtering Items
The cart can be filtered by any supplied key and value with the method.
Accessing Item counts
The method will return an item count excluding quantities.
The method will return an item count including quantities.
A convenience method is built into the cart, this proxies through to the getTotalUniqueItems with a === 0 check.
Accessing prices
Get the overall price including tax with the method.
Occasionally dealing with tax isn't required, in this case you can use the method.
Get the total cart tax.
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.
You can subscribe to these events by attaching listeners to the cart via .
All events have built in methods to retrieve the item triggering the event and the cart itself.
cart.add
cart.update
cart.remove
All versions of cart with dependencies
aura/session Version ^2.0
league/event Version ^2.1
moneyphp/money Version ^3.0