7 Replies - 2516 Views - Last Post: 02 January 2010 - 09:21 AM Rate Topic: -----

#1 madpotatokipp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-January 09

PHP Shopping Cart Help

Post icon  Posted 28 December 2009 - 10:28 PM

This is my "add to cart" page. It retrieves data from the previous page. Every time an item is added to the cart, I need to determine if the item already exists in the cart and change only the quantity amount + 1. How would I modify this code to do such a thing. Thanks in advance.

session_start();

$category = $_GET['category'];
$product = $_GET['product'];
$productprice = $_GET['price'];

				$_SESSION['cart'][] = array(
							"productkey" => $product,
							"productquantity" => 1,
							"productimage" => "<img src='../images/".$category."/".$product.".jpg' />",
							"productname" => $product,
							"productprice" => $productprice
							);

This post has been edited by madpotatokipp: 28 December 2009 - 10:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP Shopping Cart Help

#2 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP Shopping Cart Help

Posted 28 December 2009 - 10:46 PM

you can use in_array to check if item is already in cart session or not :)
Was This Post Helpful? 0
  • +
  • -

#3 madpotatokipp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-January 09

Re: PHP Shopping Cart Help

Posted 01 January 2010 - 10:24 PM

How would I write the code to only check if the product passed into the addto script is in the cart session? If it is, I only need to change to quantity for that session item. Or else create new item in the cart.

View Postnoorahmad, on 28 Dec, 2009 - 09:46 PM, said:

you can use in_array to check if item is already in cart session or not :)

Was This Post Helpful? 0
  • +
  • -

#4 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP Shopping Cart Help

Posted 01 January 2010 - 11:38 PM

I will show an example how to check for item in array if exists increase quantity.
First check if session not exists then create a session.
then check if session is not empty, if not empty the run a loop to check each name if name exists then increase quantity, if name not exists then add the new value to session
<?PHP
session_start();

$category = "Food";
$product = "Meet";
$productprice = 150;

if(!isset($_SESSION['cart'])){
	$_SESSION['cart'] = array();
}
$newArray = array();

if(count($_SESSION['cart']) >0 ){
	foreach($_SESSION['cart'] as $sessionArray){
		$found = false;
		if($sessionArray['productname'] == $product){
			$sessionArray['productquantity'] += 1;
			$found = true;
		}
		$newArray[] = $sessionArray;
	}
		if($found == true){
			$_SESSION['cart'] = $newArray;
		}else{
			$_SESSION['cart'][] = array(
						"productkey" => $product,
						"productquantity" => 1,
						"productimage" => '<b>Image</b>', #"<img src='../images/".$category."/".$product.".jpg' />",
						"productname" => $product,
						"productprice" => $productprice
						);
		}
}
else{	
	$_SESSION['cart'][] = array(
				"productkey" => $product,
				"productquantity" => 1,
				"productimage" => '<b>Image</b>', #"<img src='../images/".$category."/".$product.".jpg' />",
				"productname" => $product,
				"productprice" => $productprice
				);
}
echo "<pre>".print_r($_SESSION['cart'],true)."</pre>";
?>


Hope it hepls :)

This post has been edited by noorahmad: 01 January 2010 - 11:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 madpotatokipp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-January 09

Re: PHP Shopping Cart Help

Posted 02 January 2010 - 12:09 AM

Everything works fine except for when you add the first product continuously, it will increase correctly, but when you add a different product then try to add the first product again it will duplicate the first product and change the quantity of both lines by one. Any suggestion on how to fix that problem? Thanks again.

View Postnoorahmad, on 1 Jan, 2010 - 10:38 PM, said:

I will show an example how to check for item in array if exists increase quantity.
First check if session not exists then create a session.
then check if session is not empty, if not empty the run a loop to check each name if name exists then increase quantity, if name not exists then add the new value to session
<?PHP
session_start();

$category = "Food";
$product = "Meet";
$productprice = 150;

if(!isset($_SESSION['cart'])){
	$_SESSION['cart'] = array();
}
$newArray = array();

if(count($_SESSION['cart']) >0 ){
	foreach($_SESSION['cart'] as $sessionArray){
		$found = false;
		if($sessionArray['productname'] == $product){
			$sessionArray['productquantity'] += 1;
			$found = true;
		}
		$newArray[] = $sessionArray;
	}
		if($found == true){
			$_SESSION['cart'] = $newArray;
		}else{
			$_SESSION['cart'][] = array(
						"productkey" => $product,
						"productquantity" => 1,
						"productimage" => '<b>Image</b>', #"<img src='../images/".$category."/".$product.".jpg' />",
						"productname" => $product,
						"productprice" => $productprice
						);
		}
}
else{	
	$_SESSION['cart'][] = array(
				"productkey" => $product,
				"productquantity" => 1,
				"productimage" => '<b>Image</b>', #"<img src='../images/".$category."/".$product.".jpg' />",
				"productname" => $product,
				"productprice" => $productprice
				);
}
echo "<pre>".print_r($_SESSION['cart'],true)."</pre>";
?>


Hope it hepls :)

This post has been edited by madpotatokipp: 02 January 2010 - 12:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP Shopping Cart Help

Posted 02 January 2010 - 12:28 AM

move $found variable before foreach loop and use a condition if $found is false then add else don't

Try this code:
<?PHP
session_start();
$category = "Food";
$product = "Meet";
$productprice = 150;

if(!isset($_SESSION['cart'])){
	$_SESSION['cart'] = array();
}
$newArray = array();

if(count($_SESSION['cart']) >0 ){
	$found = false;
	foreach($_SESSION['cart'] as $sessionArray){
		echo $sessionArray['productname']."<br/>";
		if($found == false){
		if($sessionArray['productname'] == $product){
			$sessionArray['productquantity'] += 1;
			$found = true;
		}}
		$newArray[] = $sessionArray;
	}
		if($found == true){
			$_SESSION['cart'] = $newArray;
		}else{

			$_SESSION['cart'][] = array(
						"productkey" => $product,
						"productquantity" => 1,
						"productname" => $product,
						"productprice" => $productprice
						);

		}
}
else{	
	$_SESSION['cart'][] = array(
				"productkey" => $product,
				"productquantity" => 1,
				"productimage" => '<b>Image</b>', #"<img src='../images/".$category."/".$product.".jpg' />",
				"productname" => $product,
				"productprice" => $productprice
				);
}
echo "<pre>".print_r($_SESSION['cart'],true)."</pre>";
?>

Was This Post Helpful? 1
  • +
  • -

#7 madpotatokipp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-January 09

Re: PHP Shopping Cart Help

Posted 02 January 2010 - 09:00 AM

Thank you so much noorahmad. The code works perfectly.
Was This Post Helpful? 0
  • +
  • -

#8 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP Shopping Cart Help

Posted 02 January 2010 - 09:21 AM

Glad your problem solved :)
Now can you change the title of this topic and Solved word, it will help other users when they are searching here.

PHP Shopping Cart Help ((Solved))

This post has been edited by noorahmad: 02 January 2010 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1