|
-
May 31st, 2009, 10:08 PM
#1
Thread Starter
Member
[RESOLVED] Stock control
Hi everyone,
I was hoping I could get some ideas/suggestions on stock control.
Say I have 5 of an item in stock, if a customer selects 6 on the product page, when it goes through my "add item to cart" code, I also need it to:
1) Check the amount added against the stock level
2) If its more then the stock level, put the max amount available (cart quantity)
3) Display message to customer, something like "Quantity selected exceeds maximum allowed amount."
Then later on, when they send the order I guess I submit the stock then.
Does that make sense? Is there a better way to do it?
This doesn't seem to hard, its just working it in with my current script.
I had a lot of help from kows and I dont' want to break it 
(I have a field called stockamount in the product table)
Code (some code changed):
PHP Code:
<?php
if(isset($_REQUEST['productid'], $_REQUEST['quantity']) && is_numeric($_REQUEST['productid']) && is_numeric($_REQUEST['quantity'])){ //product id is set, check if there is currently an order for this person if(isset($_SESSION['OrderID']) && $_SESSION['OrderID'] > 0){ //already has an order, so we either add or update the order
//first check if this product exists on this person's order extract(mysql_fetch_assoc(mysql_query("SELECT COUNT(*) as num FROM orderitem WHERE OrderID_FK='{$_SESSION['OrderID']}' AND ProductID_FK='{$_REQUEST['productid']}' LIMIT 1"))); if($num > 0){ //this item DOES exist on this person's order, so we increase its quantity. $order_sql = "UPDATE orderitem SET OrderItemQuantity=(new quantity and price') AND id values LIMIT 1"; echo 'updated quantity of your existing order'; }else{ //this item does NOT exist on this person's order, so we need to get product information and insert a new orderitem //getting product information (you will most likely need to change this, no idea how you store it) extract(mysql_fetch_assoc(mysql_query("SELECT Price FROM product WHERE ProductID='{$_REQUEST['productid']}' LIMIT 1"))); if($ProductPrice > 0){ //insert a new order $order_sql = "INSERT INTO orderitem (order details) VALUES('order values')"; echo 'added a new item to an existing order'; }else{ echo "that product doesn't exist! 2"; } } }else{ //new user, new order //first, we create a new order mysql_query("INSERT INTO orders (CustID, Auth) VALUES('{$_SESSION['customerid']}', '0')"); //get that order id $_SESSION['OrderID'] = mysql_insert_id(); //get product information extract(mysql_fetch_assoc(mysql_query("SELECT Price FROM product WHERE ProductID='{$_REQUEST['productid']}' LIMIT 1"))); if($ProductPrice > 0){ //now we add this item to the order $order_sql = "INSERT INTO orderitem (order details) VALUES('order values')"; echo 'added a new order'; }else{ echo "that product doesn't exist! 1"; } } //we're done. mysql_query($order_sql); //inform customer product has been added to cart ?> <?php } ?>
Last edited by buffy; May 31st, 2009 at 10:56 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|