Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

How to show Item In Cart & prevent ordering again


fdcusa

Recommended Posts

My stock is for the most part a quantity of (1). I'm having a problem where Customer A add it to their cart and is active, then Customer B comes along and is able to add the same item to their cart, but does not get notification that it is out of stock until Check-Out. How can Customer B be prohibited from adding it to the cart in the first place?

Link to comment
Share on other sites

The alternative could actually be worse. Suppose Customer A adds a product to their cart and then leaves. Now Customer B tries to buy the product and can't. You've just lost a sale. Isn't that worse than an annoyed customer who just missed out on a one-of-a-kind product?

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Thanks for the reply, Jim.

 

As a buyer, I would be more inclined to shop an online store where if I added items to my cart, I knew I had a period of perhaps 2-4 hours (defined by Admin) to place the order without worrying of someone getting it out from under me - like reserving the item. The thought is something like a CRON job running that would delete carts that have been inactive for an Admin-specified period of time. When the cart is deleted, then someone else can add the same item with qty=1 to their cart.

Link to comment
Share on other sites

If you're not worried about losing the sale, then what you described should not be that hard to code. First, you need to reduce the stock by 1 for each item added to the cart. The add_cart() method in the shoppingCart class (catalog/includes/classes/shopping_cart.php) is the place I would add this. You should also remove/comment out the code in checkout_process.php that reduces the stock quantity:

        tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = '" . $stock_left . "' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");

 

You could (as you suggested) run a CRON job periodically to clean up abandoned carts. To do this, you'll need to add a database field, probably to the products table, for a timestamp on the above action. You'll also need a Sold flag, updated in checkout_process.php. Then have your CRON job compare the timestamps to the current time, and check the Sold flag, and reset any that are too old and not sold. Or you could put the check function in the cart class so it executes every time someone adds a product to their cart. This eliminates the need for the CRON, but adds a bit of overhead.

 

That's the way I would do it anyway. You may well come up with something neater. Please share if you do -- this is an interesting problem.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Thanks again Jim for the ideas.

 

I got to thinking, though, if the item isn't checked-out, I really don't want to take the quantity from the products_quantity. Then I found table Customers_Basket, which appears to me to be where the "What's In My Cart" option gets the data from. So, when displaying the product_info page, for a specific product_id, I should be able to run a query to Sum the customers_basket_quantity WHERE product_id matches the product_id being displayed.

 

I'm a T-SQL guy, so not yet accustomed to getting data from mysql/php executed queries. Here is what I have so far, but I'm not getting a value returned for products_carted:

 <?php               
$products_carted = tep_db_query("SELECT Sum(customers_basket_quantity) As products_carted FROM " . TABLE_CUSTOMERS_BASKET . 
                               " WHERE products_id ='" . (int)$HTTP_GET_VARS['products_id'] . "'");
$datax =  tep_db_fetch_array($datax);
echo 'ProductID: '; echo (int)$HTTP_GET_VARS['products_id']; 
echo '  |  Carted: [<b>'; echo $datax['products_carted']; echo '</b>]';
?>

Forgot to add that I actually get an error with the above in database.php:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxx/includes/functions/database.php on line 99

Link to comment
Share on other sites

For some reason, I couldn't edit my message again...

 

I saw I had the wrong 'variable' in tep_db_fetch_array(). Changed $datax to $products_carted and now I get a value for products_carted field! Okay, stand by, I'm continuing...

Link to comment
Share on other sites

By George, I think I got it!

 

The following snipped placed in product_info.php to display in the bottom box where the "Add To Cart" button is displayed:

 <?php               
$products_carted = tep_db_query("SELECT Sum(customers_basket_quantity) As products_carted FROM " . TABLE_CUSTOMERS_BASKET . 
                               " WHERE products_id ='" . (int)$HTTP_GET_VARS['products_id'] . "'");
 $datax =  tep_db_fetch_array($products_carted);
echo 'ProductID: '; echo (int)$HTTP_GET_VARS['products_id']; 
echo '  |  OnHand: [<b>'; echo (int)$product_info['products_quantity']; echo '</b>]';
echo '  |  Carted: [<b>'; echo $datax['products_carted']; echo '</b>]';
echo '  |  Available: [<b>'; echo ((int)$product_info['products_quantity'] - (int)$datax['products_carted']); echo '</b>]';
?>

Returns this data on the page: ProductID: 640 | OnHand: [1] | Carted: [1] | Available: [0]

 

Which is CORRECT!

 

So, now all I need to do is create in IF block that evaluates the 'Available' result for whether to display "Add To Cart" or "Purchase Pending Already"

 

I'm continuing, but if anyone sees a problem with my syntax (or a better way), please let me know. Thanks!

Link to comment
Share on other sites

I got it!

                <!-- 20080531 JHW: Display Cart button ONLY if Price > 0 -->
               <?php
               if ($product_info['products_price'] > 0 ) {
               ?>
                   <!-- 20100402 JHW: Display Add To Cart ONLY if item is NOT added to a cart already -->
                   <?php               
	    $products_carted = tep_db_query("SELECT Sum(customers_basket_quantity) As products_carted FROM " . TABLE_CUSTOMERS_BASKET . 
                                                   " WHERE products_id ='" . (int)$HTTP_GET_VARS['products_id'] . "'");
	    $datax =  tep_db_fetch_array($products_carted);
	    echo 'ProductID: '; echo (int)$HTTP_GET_VARS['products_id']; 
	    echo '  |  OnHand: [<b>'; echo (int)$product_info['products_quantity']; echo '</b>]';
	    echo '  |  Carted: [<b>'; echo $datax['products_carted']; echo '</b>]';
	    echo '  |  Available: [<b>'; echo ((int)$product_info['products_quantity'] - (int)$datax['products_carted']); echo '</b>]';
	    ?>              
                   <td class="main" align="right">
                       <?php
                       if ((int)$product_info['products_quantity'] - (int)$datax['products_carted'] > 0) {
		    echo tep_draw_hidden_field('products_id', $product_info['products_id']) . tep_image_submit('button_in_cart.gif', IMAGE_BUTTON_IN_CART);  
		 } else {
		    echo 'Item is in another Shopping Cart.  Check back later in case they put it back!';
                        }?>    
                   </td> 
               <?php }?>

The debug echo statements are still there for now. Now to apply it to the Buy Now button.

 

Jim, you helped nudge me to a solution - thanks! Everyone, if you see a syntax problem above please let me know!

Link to comment
Share on other sites

I modified product_listing.php so now the Buy Now button only displays if it is not already reserved in a shopping cart; if it is reserved, "PURCHASE PENDING" is in place of the button. in the case 'PRODUCT_LIST_BUY_NOW' block:

 

            /* 20080530 JHW: Updated - if product_price > 0 then display 'Buy Now' */
           $lc_align = 'center';
           if ($listing['products_price'] > 0) {
                   /* 20100402 JHW: Display BuyNow ONLY if item is NOT added to a cart already   */
	    $products_carted = tep_db_query("SELECT Sum(customers_basket_quantity) As products_carted FROM " . TABLE_CUSTOMERS_BASKET . 
                                                   " WHERE products_id ='" . (int)$listing['products_id'] . "'");
	    $datax =  tep_db_fetch_array($products_carted);
                   if ((int)$listing['products_quantity'] - (int)$datax['products_carted'] > 0) {
                       $lc_text = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $listing['products_id']) . '">' . tep_image_button('button_buy_now.gif', IMAGE_BUTTON_BUY_NOW) . '</a> ';
                       break;
                   } else {
 	                $lc_text = 'PURCHASE PENDING';
                   }    
           } ELSE {
            $lc_text = '';
           }

As always, corrections to 'buggy code' is strongly invited!

Link to comment
Share on other sites

Looks good to me. An elegant solution indeed.

 

Regards

Jim

might be an excellent idea for a contribution. think about it!

Always backup your files! You will be glad you did

My add-ons :

SSPP Seperate Shipping Per Product v2.5| Support
Gift vouchers for SPPC 4.22 v2.1 | Support |
Catalog Infobox v1.0 | Sorry no support for Catalog Infobox |
HTML Mail v2.0 | Support |




Upcoming Add ons:

Addon Manager |
Separate Pricing Per Product Qty |
Coupon Populate |
EZ-PDF Catalog

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...