PDA

View Full Version : Preventing customers adding more to cart than in stock


CraigR
03-22-2009, 12:52 AM
Here is my method for preventing customers adding more to their cart than is available.

I'm posting this for 3 reasons...

1. To help anyone to achieve the same thing
2. To allow anyone with much better coding skills than me to (constructively) criticize the content, to help improve the code, with the ultimate aim of having an accepted solution.
3. To start the creation of unofficial 'how to' documents to round out the functionality of ecart

My eCart is called blu_basket
Here i am dealing with 2 pages...
itemdetail.php, which includes my add to cart form
basket.php, which includes all of the cart details

In basket.php, declare 2 arrays ($itemid and $itemqty) before the basket loop , then use the loop, to populate the arrays with the item ID and quantity,
$itemid = array();
$itemqty = array();
while (!$blu_basket->EOF()) {//existing line
$itemid[] = $blu_basket->DisplayInfo("ID");
$itemqty[] = $blu_basket->DisplayInfo("Quantity");

After the line $blu_basket->MoveFirst();
at the end of the same block of php code, add the following code
$itemid_s = serialize($itemid);
$itemqty_s = serialize($itemqty);
This serializes the arrays so they can be stored as session variables, then store the serialized arrays as session variables
$_SESSION["itemid"] = $itemid_s;
$_SESSION["itemqty"] = $itemqty_s;


In itemdetail.php add the following code before the add to cart form

<?php
if (isset($_SESSION["itemqty"]) && (isset($_SESSION["itemid"]))) {//if we have (or had) something in the basket in this session
// set variables for the items and their quantities in the basket, stored as serialized arrays from basket.php in the session variables below
$itemid_s = $_SESSION["itemid"];
$itemqty_s = $_SESSION["itemqty"];
// unserialize the arrays so they are in the correct format again
$itemid = unserialize($itemid_s);
$itemqty = unserialize($itemqty_s);
$itemidkey = array();//initialise an array, (Required in case item has been added to the basket then removed)

foreach ($itemid as $key => $value) {// loop through the items in the basket
if ($value == $row_rsItemDetail['ItemID']) {// if the current itemid is there **
$itemidkey[$key] = $value;//create a new array for all instances of this itemid, putting the keys where
// they occur in an associative array so they match up with the quantities in the $itemqty array
}
}
$matchqty = array_intersect_key($itemqty,$itemidkey);// where they keys match, we get the quantities for this itemid
$qtyinbasket =(array_sum($matchqty));
//echo $qtyinbasket; //print variable until you are happy all is working
}
else {
$qtyinbasket = 0 ;
}
//** if there is no matching itemid in the basket, then the variable $qtyinbasket will be 0
// use this variable to enable / disable the add to cart button
//job done !!

Jon Gibson
03-22-2009, 01:02 PM
Great idea Craig . . . I am not sure how to make a link to a thread I have that may help others, but it is the Powerful Product for Simple Problem thread. It covers working with Authorize.net and creating a simple donation cart.

Many thanks for sharing your solution . . . the code looked good to me (I am still very new to PHP and SQL).

Sades
01-26-2010, 10:34 PM
i been trying to do this but the disable button only works after submiting to the cart maby im doing something wrong, help please this is how i have it on the product or detail page of an item i have the code of itemdetail.php you place there just before

<form name="cart_1_ATC_........

the other one is on where i display the ecart following your instructions.

now it doesnt disable my submit to cart button if im on the page for the first time and after i submit and if i hit continue shoping if i added more than whats in the stock it disables the button but then it is always is disabled unless i clear session variables, how can i get this done the first time? or how can i disable the check out and give a msg that they are adding more than they should?

CraigR
01-27-2010, 01:13 AM
without seeing your code it is difficult to determine what you are doing wrong.

Ignoring the code checking the stock quantity for the time being, get your page working with the 'add to cart' button enabled / disabled functionality.

to do this, create a php variable and give it a value > 0

then create a php if statement, something like...

<?php
if (php variable == 0) { ?>
**code for disabled button**
<?php
}
else {
?>
**code for normal button**
<?php
} ?>

this should give you a page with the 'add to cart' button enabled.

Try changing your php variable to 0, this should give you the 'add to cart' button disabled.

when you get this working ok, replace the php variable with the code that compares the cart quantity against the stock quantity

Office Guy-172461
01-27-2010, 06:32 AM
I'm posting this for 3 reasons...

3. To start the creation of unofficial 'how to' documents to round out the functionality of ecart

Hey Craig,

I have a collection of user created How Tos here:
http://ipvac.com/kb/34/

I only add them at the request of the author. Let me know if you'd like this to be included.

CraigR
01-27-2010, 08:29 AM
looking at the code I posted, it may be a bit daunting for those with no coding experience, though should be straightforward for those who have some php.

if you think it would help, when i get time, i may spend some time walking through the process in stages, which would be more user friendly.

in the meantime, i am happy for you to post the code.

Office Guy-172461
01-27-2010, 08:54 AM
Thanks Craig,

here's the link:
http://www.ipvac.com/kb/entry/306/

The main idea is to save people time by giving them a starting point or an idea of how to do things. I update articles as more information becomes available or I get feedback.

You can use the Contact Us button on the home page whenever you want me to make updates or changes in case I miss it in the forum.

Sades
01-27-2010, 12:32 PM
here are the page codes, i also added a javascript that allows users to place from 1 producto up to the max number of amount in stock, and i have comented the disable button

CraigR
01-28-2010, 02:01 AM
are you still getting a problem with a disabled button ?

i downloaded the file and got the page to load, can you explain where you have an issue ?

thanks