Jump to content

PHP MySQL shopping cart quantity control


lionshead86

Recommended Posts

Hi,  I'm building a site for my friend and up until now I've only dealt with HTML & CSS.  So far so good with PHP & SQL but now I'm trying to build a shopping cart and I think I'm lost.  I've used code from a shopping cart tutorial to work with my site but I need to modify it to control for the available number on hand for each product.

 

In the SQL database I have a products table and one of the fields is "number_on_hand".  I've been able to work with that so that when it reaches zero "temporariily unavailable" shows up on the shopping pages instead of "add to cart".

 

Now, for the actual cart itself, I want to prevent people from updating the quantity in their cart to a number higher than the available number on hand.

 

This is the code executed when someone clicks "update cart":

 

	case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}

 

I think that I basically need to insert something like this:

 

   

foreach ($_POST as $key=>$row['number_on_hand]') {
             value = $['number_on_hand'];

 

 

But no matter what  I try I keep getting errors.  Any suggestions?

 

Thanks

 

ps:  the site is jungleemporium.com if that's helpful

I don't think $_POST as $key=>$row['number_on_hand]' is what your looking for. 

 

It's hard to tell from your block of code but you need to check if the quantity is above 'number_on_hand'

 

something along the lines of

 

if($qty > row['number_on_hand']){
$qty = row['number_on_hand]');
$message = 'Max quantity available in basket';
}else{
updatecart(...)
}

 

Hopefully that helps a small amount.  You're going to have to query your database for the number_on_hand'...

 

Are you using paypal?

Thanks cmor, I think that makes more sense.    If I can pull this off I hope to give the user the option of checking out with PayPal or Google checkout.  I might even look into using a merchant ID if I'm feeling confident. 

 

I'm not too worried, though.  Worst case scenario  I'll have to settle for PayPal's button codes but it would be easier for my friend if he could just add the products through the forms I've set for him in the admin panel.

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.