Jump to content

[SOLVED] Shopping Carts differences in the same script...


gerkintrigg

Recommended Posts

I have been making shopping carts for years, and previously used the same script on http://www.tingifts.co.uk/, which works fine. but for some reason on http://jewellerybasket.com/, the shopping cart just doesn't add anything to the variable. and I don't know why.

 

The shopping cart script is here:

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

if(!empty($_REQUEST['prod'])){
$new=$_REQUEST['prod'];
if(!empty($_POST['a'])){
	if ($_POST['a']=='a'){
		foreach ($_SESSION['cart'] as $my_new => $qty){
			if (($_POST[$my_new]=='')||($_POST[$my_new]=='0')){
				unset($_SESSION['cart'][$my_new]);}
			else{
				$_SESSION['cart'][$my_new]=$_POST[$my_new];
			}
		}
	}
	else{
		if (isset($_REQUEST['prod'])){
			if(isset($_SESSION['cart'][$new])){
			 $_SESSION['cart'][$new]=$_SESSION['cart'][$new]+1;
			}
		    else{
		    	$_SESSION['cart'][$new] = 1;
			}
		}
	}
}
}
?>

the shopping cart just doesn't add anything to the variable.

 

It would be nice to know to which variable you are referring, plus have you started sessions? If not then $_SESSION is empty on each new request, which would explain why nothing is added.

I defined sessions on every page. The site that isn't working is almost an exact copy of the site that is working...

The only differences are a slight change to the navigation and a search facility added.

 

Sorry, the cart items should be coming directly from the $_SESSION['cart'] variable (an array). It would appear to be empty.

I confess, I have no idea what happened to that script... but it's working again now, after much playing.

 

Here's the completed version... though it's not as elegant as I'd like.

 

<?php 
# if there is no cart session open, then open one:
if (! isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}

# if we are adding a new product to the cart:
if(!empty($_REQUEST['prod'])){
#set the new product
$new=$_REQUEST['prod'];
# check to see if the security field is correct:
if(!empty($_POST['a'])){
	# security check to see if the quantity needs updating:
	if ($_POST['a']=='a'){
		#run through the cart and check the quantities:
		foreach ($_SESSION['cart'] as $my_new => $qty){
			#if the product quantity is empty or set to zero:
			if (($_POST[$my_new]=='')||($_POST[$my_new]=='0')){
				# delete the product from the cart:
				unset($_SESSION['cart'][$my_new]);
			}
			#otherwise
			else{
				# update the quantity
				$_SESSION['cart'][$my_new]=$_POST[$my_new];
			}
		}
	}
	else{
		if (isset($_REQUEST['prod'])){
			if(isset($_SESSION['cart'][$new])){
			 $_SESSION['cart'][$new]=$_SESSION['cart'][$new]+1;
			}
		    else{
		    	$_SESSION['cart'][$new] = 1;
			}
		}
	}
}
else{
	$_SESSION['cart'][$new] = 1;
}
}

foreach ($_POST as $key => $value){
if ($key!='a'){
	if (!empty($key) && (($value!='0')&&($value!=''))){
		$_SESSION['cart'][$key]=$value;
	}
	else{
		unset($_SESSION['cart'][$key]);
	}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.