SPONSORS

Deploy Your Apps on AWS Cloud
2175 views · 125 days ago

![Standalone PHP Class for Managing Session Based Multiple Cart](https://images.ctfassets.net/vzl5fkwyme3u/5eJ0LsF3z7wkHhDaRBVCtM/1914842168ffa3b33109e930fda9fcd8/AdobeStock_214539382.jpeg?w=1000)

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

```php

require 'PhpKart.class.php';

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### 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

```php

$obj1 = new PhpKart("MainCart");

```

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

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### Adding any product inside cart

```php

$obj1->add_to_cart($product_id=123,$quantity=2,$product_information=array("product_name"=>"Tomato","Price"=>20));

```

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

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

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

```php

$obj1->add_to_cart($product_id=123,$quantity=5);

```

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

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### If we want to add another product:

```php

$obj1->add_to_cart($product_id=145,$quantity=3,$product_information=array("product_name"=>"Potato","Price"=>30));

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### 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:

```php

$already_exists=$obj1->AlreadyExists($product_id=145);

if($already_exists !== false )

{

print_r($already_exists);

}

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### If we want to remove any product from cart:

```php

$obj1->remove_from_cart($product_id=145);

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### If we want to get all products from the current cart

```php

print_r( $obj1->getAllItems() );

```

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

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### Getting all item's count inside the current cart

```php

echo $obj1->getTotalItems();

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

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

```php

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

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

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

```php

$obj1->clear_cart();

```

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:

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### Create new Object by passing different Key

```php

$obj2=new PhpKart("MyWishlist");

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

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

```php

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

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

#### Show the total cart Amount

```php

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

```

![](https://images.ctfassets.net/vzl5fkwyme3u/3dJiwaSO68OVV0OLHX8HJO/59d5c9c08427ad7c4c0a036523191a7b/transp.png?h=20)

## 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:

```php

<?php

if(!session_id()){

@session_start();

}

/ *

@file name: PhpKart.class.php

@author: Jeetendra Singh

@Contact: [email protected]

@description: Multiple Session key Based cart dta management class

*/

class PhpKart{

private $cart_key;

public function __construct($CartkeyName)

{

$this->cart_key=$CartkeyName;

}

public function add_to_cart($product_id,$quantity='1',$product_information=array())

{

$_SESSION[$this->cart_key][$product_id]['product_id']=$product_id;

$_SESSION[$this->cart_key][$product_id]['quantity']=$quantity;

if(!isset($_SESSION[$this->cart_key][$product_id]['product_information']) && !empty($product_information)){

$_SESSION[$this->cart_key][$product_id]['product_information']=$product_information;

}

}

public function remove_from_cart($product_id)

{

unset($_SESSION[$this->cart_key][$product_id]);

}

public function clear_cart()

{

unset($_SESSION[$this->cart_key]);

}

public function getTotalItems()

{

return count($_SESSION[$this->cart_key]);

}

public function getCartTotalAmount($price_key_name)

{

/ / price key name inside the product_information

$total=0;

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];

}

else

{

return false;

}

}

public function getAllItems()

{

return $_SESSION[$this->cart_key];

}

}

?>

```

SPONSORS

Faster PHP Cloud Hosting