Show Stock Availability in osCommerce Product Info
Buy Gary A Beer?
Buying me a "beer" helps me to keep my contributions updated and keep this blog alive - and you get a link from my homepage to your site. Cheers!
Dana asks;
Im trying to show the amount of items that I have available on each product page please help
This is an often asked question and is really simple to do. Open up product_info.php and add this code somewhere:
This is a standard php function and will output (in numbers) the amount of product you have in stock. Couldn't be easier really.
How to make this more friendly
How about having a series of messages showing availability rather than just numbers...one way to do this would be to make our own function which uses the existing tep_get_products_stock function and adds in a few pieces of php of our own...
Step 1 - the new function
So, open up /includes/functions/html_output.php and add this code:
-
function clubosc_products_stock($pID) {
-
switch(tep_get_products_stock($pID)) {
-
case 10:
-
case 9:
-
case 8:
-
case 7:
-
case 6:
-
$in_stock = TEXT_LIMITED_AVAILABILITY;
-
break;
-
case 5:
-
case 4:
-
case 3:
-
case 3:
-
$in_stock = TEXT_VERY_LIMITED_AVAILABILITY;
-
break;
-
case 1:
-
$in_stock = TEXT_ULTRA_LIMITED_AVAILABILITY;
-
break;
-
default:
-
$in_stock = TEXT_UNLIMITED_AVAILABILITY;
-
}
-
return $in_stock;
-
}
What we have done here is create a NEW function called clubosc_products_stock - you could name this absolutely anything to suit your needs. This new function uses the existing function I already spoke of above, to get the number of products in stock. It then compares that number against a "case" and shows the relevant message! Easy as 123. No?
Basically the "case" is saying IF the number in stock EQUALS me, then show a message. In the example above, if the number in stock is 1, then the message TEXT_ULTRA_LIMITED_AVAILABILITY will show. If the number in stock is 2000, then TEXT_UNLIMITED_AVAILABILITY will show as the message.
Got it now?
Step 2 - language
Next up is to add those messages, so open up /includes/languages/{ your language }/product_info.php and add:
Obviously repeat in each language that your shop uses.
Step 3 - add the function to the product page
Open up product_info.php and add this code somewhere:
Save it all, reload your page, and see what shows. Easy as 123.
Change the SWITCH
So, let's say you want to add a new message inside the switch, with a new message for stock between 15 and 11 items available. Easy, just add a new BLOCK of code inside the switch, like this:
-
case 15:
-
case 14:
-
case 13:
-
case 12:
-
case 11:
-
$in_stock = TEXT_SORTOF_LIMITED_AVAILABILITY;
-
break;
And remember to also add the new language piece!
Show Images instead of Text
Easy enough, just create the images you need for each BLOCK of availability and set the image (instead oft ext) as the $in_stock - like this:
-
$in_stock = tep_image(DIR_WS_IMAGES . 'loads.gif');
Have fun!



[...] a bit of thought, I adapted a previous idea of mine, blogged here. And ended up with a good way of showing what's in stock, what's limited [...]
Pingback by Club osCommerce » Traffic Lights for showing Stock in osCommerce — March 24, 2009 @ 10:59 am
What if i am using negative numbers for stock as well? what should the "case 13:" look like?
Comment by Mike — November 5, 2009 @ 2:40 pm
case "-13":
might work, I haven't tested it.
Comment by Gary — November 10, 2009 @ 1:02 pm
yes that works…But I would really like to include all negative numbers. Something like case: <0 but i am not really sure of the correct syntax of the statement. Could you please give me a hand?
Comment by Mike — November 10, 2009 @ 1:06 pm
You could use the default for anything below 1. Experiment, read the PHP page about "switch" function…learn from your mistakes.
Comment by Gary — November 10, 2009 @ 1:24 pm
is there any way of displaying the stock status in the products_info.php page ONLY when the user is a registered member?(logged in)
Comment by Mike — November 30, 2009 @ 8:57 am
Mike; http://www.clubosc.com/if-something-do-this-else-do-that.html should give you the right idea.
Comment by Gary — November 30, 2009 @ 6:06 pm