Jump to content

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]);
	}
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.