A shopping cart class implementation in PHP
It allows basic operations of a shopping cart; ie adding, removing, updating cart item through its quantity, listing all items in the cart, clearing items in the cart as well as calculating total cost of items in the cart.
Instantiate a Cart object passing in your source data. In a real application, you won't be passing data to the cart though. And in that case the constructor will not be needed.
$products = [
[
'id' => 1,
'name' => 'Smart Watch',
'price' => 45,
'description' => 'This Fitness Bracelet has a high sensitive touch screen'
],
...
];
$cart = new \App\Cart($products)
To test the sample application built with this class, clone the repository and run:
php -S localhost:8000 -t examples
Methods used by Cart class are pretty self explanatory
$cart->add($id, $quantity)
$cart->remove($id)
$cart->update($id, $quantity)
$cart->all()
$cart->count()
$cart->sum()
$cart->remove($id)
$cart->clear()