You are on page 1of 4

Simple Shopping

Cart class v0.1


by Alberto Ruffo






If you need help, contact me at:

me@albertoruffo.it





















1





Useful methods

First of all, you need to require the class le:

require cart.class.php;

Then, create an object:

$cart = new Cart();

To add items to the cart, you need to implement the Addable interface, there are 3 methods to be
dened:

public function getPrice();

public function getAvailableQty();

public function getCode();

The rst method returns a oat, for example ( 24.39), the second one returns an integer and the third a
string. This code must be unique, it could be an id eld of a MySQL table or the product code itself.

Look at the example.php to get more infos.

To add an item to the cart, use:

$cart->addItem($product);

You can also set a dened quantity, if the quantity is not available for that product, the cart will set the
maximum available quantity for the item:

$cart->setItem($product, 5);

To delete an item use:

$cart->deleteItem($product);

or simply:

2
$cart->deleteItem($cart->getItemByCode($your_desired_product_code));

To get the total amount, print:

$cart->getTotal();

To get how many different products are in the cart use:

$cart->size();

To get how many products are in the cart use:

$cart->fullSize();

Check if the cart is empty using:

$cart->isEmpty();

it will return a boolean value.

You can trash your cart using:

$cart->trash();

You can get an array of items:

$cart->getItems();

Each item is an object of the Item class, from the Item you can return your beginning Addable object
using something like this:

$your_addable_product = $cart->getItemByCode($your_desider_product)->getProduct();
echo $your_addable_product->getTitle();

This is very useful when you want to print all the items and their features at the
same time.

Do you want to get the quantity of a dened Item in the cart? You can write this:

$qty = $cart->getItemByCode($id)->getQty();

Youd need to export your shopping cart in the session so that you can call your shopping cart object in
another page:

3
$_SESSION['cart'] = $cart->getSerialized();
$cart = Cart::getUnserialized($_SESSION[cart']);
4

You might also like