Simple shopping cart system.
##Usage
####Adding product to cart
Use the method \Gabrieljmj\Cart\Cart::add(ProductInterface $product)
to add a product. If previously the same product was added, will be add one more to cart.
use Gabrieljmj\Cart\Product\Product;
use Gabrieljmj\Cart\Cart;
$product = new Product(1, 'TV', 499.90, ['tv', 'led']);
$cart = new Cart();
$cart->add($product);
####Removing product of a cart
Using \Gabrieljmj\Cart\Cart::remove($product[, $amount = 0])
you can remove products of a cart. If $amount
be 0
, all products of that products will be removed. The argument $product
can be an instance of \Gabrieljmj\Cart\Product\ProductInterface
or product id.
$cart->remove(1);
####Clearing the cart
To this use the method \Gabrieljmj\Cart\Cart::clear
.
$cart->clear();
####Verifying if the cart has a product
The $product
argument can be the product id or an instance of \Gabrieljmj\Cart\Product\ProductInterface
. The return will be boolean.
$cart->has($product);
####Counting how many products has in the cart
The method \Gabrieljmj\Cart\Cart::count()
will return how many items has in the cart.
$cart->count();
####Counting how many products of a type has in the cart
Use the method \Gabrieljmj\Cart\Cart::getTotalOfAProduct($product)
and like others, $product
can be an instance of \Gabrieljmj\Cart\Product\ProductInterface
or product id.
$cart->getTotalOfAProduct($product);
####Counting how many types of products has in the cart
And this method (\Gabrieljmj\Cart\Cart::countUniqueItems()
) counts how many types of products has in the cart.
$cart->countUniqueItems();
####Calculating total price of the cart
The method \Gabrieljmj\Cart\Cart::getTotalPrice()
returns how much costs the cart.
$cart->getTotalPrice();
####Iterating with all products
Each product will return an instance of \Gabrieljmj\Cart\Product\ProductInterface
:
$iterator = $cart->getIterator();
while ($iterator->valid()) {
$curr = $iterator->current();
echo '<li><b>Product:</b>' . $curr->getProduct()->getName() . ' / <b>Total:</b> ' . $cart->getTotalOfAProduct($curr) . '</li>';
$iterator->next();
}
####Storaging The instance of the cart usually is save on a session or a cookie.
$_SESSION['cart'] = $cart;
//or
setcookie('cart', $cart);