close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Preventing customers adding more to cart than in stock

Thread began 3/22/2009 12:52 am by CraigR | Last modified 1/28/2010 2:01 am by CraigR | 4590 views | 8 replies |

CraigRBeta Tester

Preventing customers adding more to cart than in stock

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 !!

Sign in to reply to this post

Jon GibsonBeta Tester

Awesome

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).

Sign in to reply to this post

Miguel

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?

Sign in to reply to this post

CraigRBeta Tester

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

Sign in to reply to this post

Office Guy-172461

Originally Said By: CraigR
  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:
34/

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

Sign in to reply to this post

CraigRBeta Tester

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.

Sign in to reply to this post

Office Guy-172461

Thanks Craig,

here's the link:
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.

Sign in to reply to this post

Miguel

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

Attached Files
edit.zip
Sign in to reply to this post

CraigRBeta Tester

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

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...