PHP & Web Development Blogs

Search Results For: standalone
Showing 1 to 1 of 1 blog articles.
7285 views · 4 years ago
Standalone PHP Class for Managing Session Based Multiple Cart

Hi Guys,

I am sharing you a standalone class for managing a session based cart system. In this class I have provided multiple methods for adding, updating and deleting the products. and By using this class you may manage multiple cart objects with different data only you need to pass a different key in constructor of that class.

Let me share you the How can we use that Class:

Include The cart class

require 'PhpKart.class.php';

Create a Cart Object initializing the Cart base key name, by using different keys we can manage multiple cart data in a same project , like main cart or wishlist

$obj1 = new PhpKart("MainCart");

Here Maincart is the keyname in which $obj1 insert/modify/delete the data

Adding any product inside cart


Where parameter1 is product, id,parameter 2 is quantity, and Parameter 3 is product information Array

Now If we want to update the quantity in product_id=123 with quantity 5 then use following:


Here we did not pass the information array again because no matter you pass or not if the product information is already added in the cart then if won't be affected So no ned to pass this, only pass product id and its Quantity

If we want to add another product:


Now we have a case what if our product is already having some quantity and we want to increase into it or decrease into it, Then to retrieve the already added product information, using AlreadyExists method by passing product id,it will return array if it exists and it will return boolean false if not exists:

if($already_exists !== false )

If we want to remove any product from cart:


If we want to get all products from the current cart

print_r( $obj1->getAllItems() );

It will return you an array with all the items exist in the current cart

Getting all item's count inside the current cart

echo $obj1->getTotalItems(); 

If we want to get calculate total amount in the current cart

echo $obj1->getCartTotalAmount($priceKeyName='Price')

Note: In the above snippet we need to provide teh same key name which we have used for product price in project information array at the time of adding items in cart array, and need to take the same key name in all the products inserting in Same Cart

If we want to clear/reset the current cart then use following:


Now if we want to save some other user interested items separately which use likes but don't want to buy now so that items can be Add into wishlist So we need to create new object with different cart ket name and that list will be manage separately using it's object. Like Following:

Create new Object by passing different Key

$obj2=new PhpKart("MyWishlist"); 

Add a new item to $obj2 object containing the items user want to add in his/her wishlist

$obj2->add_to_cart($product_id=159,$quantity=8,$product_information=array("product_name"=>"Baby Toy","Price"=>100));

Show the total cart Amount

echo $obj2->getCartTotalAmount("Price"); 

Full Source Code

Following is the full code for that cart managing Class . Just create a file named PhpKart.class.php and add following code in it:


class PhpKart{

private $cart_key;

public function __construct($CartkeyName)
public function add_to_cart($product_id,$quantity='1',$product_information=array())
if(!isset($_SESSION[$this->cart_key][$product_id]['product_information']) && !empty($product_information)){
public function remove_from_cart($product_id)
public function clear_cart()
public function getTotalItems()
return count($_SESSION[$this->cart_key]);
public function getCartTotalAmount($price_key_name)
foreach($_SESSION[$this->cart_key] as $row)
$total += ($row['product_information'][$price_key_name]*$row['quantity']);

return $total;
public function AlreadyExists($product_id)
if (isset($_SESSION[$this->cart_key][$product_id]))
return $_SESSION[$this->cart_key][$product_id];
return false;
public function getAllItems()
return $_SESSION[$this->cart_key];




    PHP Tutorials and Videos