Jump to content

[SOLVED] Problem Reducing Product Quantity


stevesimo

Recommended Posts

Hi, I have a simple shopping cart which maintains a string of product codes and stores this in a session variable.  For example the string: abbccc contains 1 * product a, 2 * product b, and 3 * product c

 

What I am trying to achieve is when the user proceeds to the checkout, I am checking the quantity in the basket for each product against the actual product quantity in stock.  If the quantity in the basket is more than the quantity in stock then I want to set the quantity in the basket equal to the product stock level so as to prevent selling a product quantity which isnt in stock.

 

Im just not sure how to adapt my code to do this.  Here is my code:

 

$postage = 5.99;

global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<table width="100%">';

	//Table Column Headings
	$output[] = '<tr>';
	$output[] = '<td width="15%"><font color="red"><strong>Code</strong></font></td>';
	$output[] = '<td width="40%"><font color="red"><strong>Description</strong></font></td>';
	$output[] = '<td width="15%"><font color="red"><strong>Price</strong></font></td>';
	$output[] = '<td width="10%"><font color="red"><strong>Qty</strong></font></td>';
	$output[] = '<td width="20%"><font color="red"><strong>Total</strong></font></td>';
	$output[] = '</tr>';

	$message = "";

	foreach ($contents as $id=>$qty) {
	  $sql = 'SELECT * FROM Products WHERE productID = '.$id;
	  $result = $db->query($sql);
	  $row = $result->fetch();
	  extract($row);

	  //if quantity required exceeds stock level then set quantity equal to stock level to prevent over-ordering
	  if($qty > $productStock){
	    //i need to insert code here to reduce product quantity

		$message = $message.'Product: '.$productCode.' - quantity has been adjusted as the quantity required exceeds stock level<br>';
	  }//end of if quantity exceeds stock level

		$output[] = '<tr width="100%"><font size="2">';
		$output[] = '<td width="20%">'.$productCode.'</td>';
		$output[] = '<td width="30%">'.$productDescription.'</td>';
		$output[] = '<td width="15%">£'.number_format($productPrice,2).'</td>';
		$output[] = '<td width="15%">'.$qty.'</td>';
		$output[] = '<td width="20%">£'.number_format($productPrice * $qty,2).'</td>';
		$total += $productPrice * $qty;
		$output[] = '</font></tr>';
		$output[] = '<tr><td colspan="5"> </td></tr>';
	}
	$output[] = '<tr><td> </td><td> </td><td> </td><td><strong>Postage</strong></td><td><strong>£'.number_format($postage,2).'</strong></td></tr>';
	$output[] = '<tr><td colspan="5"> </td></tr>';
	$total += $postage;
	$output[] = '<tr><td> </td><td> </td><td> </td><td><strong>Cart Total</strong></td><td><strong>£'.number_format($total,2).'</strong></td></tr>';
	$output[] = '</table>';
	$output[] = '<p><br><font color="red">'.$message.'</font></p><br>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);

 

Any advice welcomed,

  thanks Steve (Blackpool)

Link to comment
https://forums.phpfreaks.com/topic/56213-solved-problem-reducing-product-quantity/
Share on other sites

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.