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:
[php][/php]
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:
[php]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;
}[/php]
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:
[php]define(‘TEXT_LIMITED_AVAILABILITY’, ‘We have a few in stock…’);
define(‘TEXT_VERY_LIMITED_AVAILABILITY’, ‘We have very few in stock…’);
define(‘TEXT_ULTRA_LIMITED_AVAILABILITY’, ‘Only 1 left, buy it now!’);
define(‘TEXT_UNLIMITED_AVAILABILITY’, ‘We have loads in stock…’);[/php]
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:
[php][/php]
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:
[php]case 15:
case 14:
case 13:
case 12:
case 11:
$in_stock = TEXT_SORTOF_LIMITED_AVAILABILITY;
break;[/php]
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:
[php]$in_stock = tep_image(DIR_WS_IMAGES . ‘loads.gif’);[/php]
Have fun!
Pingback: Club osCommerce » Traffic Lights for showing Stock in osCommerce
What if i am using negative numbers for stock as well? what should the “case 13:” look like?
case “-13”:
might work, I haven’t tested it.
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?
You could use the default for anything below 1. Experiment, read the PHP page about “switch” function…learn from your mistakes.
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)
Mike; http://www.clubosc.com/if-something-do-this-else-do-that.html should give you the right idea.
Hi,
Many thanks for this code Gary, it was just what I was after.
I had the same issue regarding negative numbers so I ammended the provided code to this
case 1:
$in_stock = TEXT_ULTRA_LIMITED_AVAILABILITY;
break;
case 0:
$in_stock = TEXT_ULTRA_LIMITED_AVAILABILITY1;
break;
case $pID>-1:
$in_stock = TEXT_ULTRA_LIMITED_AVAILABILITY1;
break;
default:
$in_stock = TEXT_UNLIMITED_AVAILABILITY;
I also added the relevent text in the language file,
worked for me – although I guess there may be a cleaner way to do it!
cheers,
anthony
Thanks Anthony 🙂
hi, could this be implemented also in includes/product_listing.php above the buy button?
thx
Hi. Yes. Amend the code to suit the ID of the product in the list.
I put your code in my website, but it always display the default code “We have loads in stock…”.
I’ve tried with other contribution and it’s the same problem, it seems to not take the stock value. Do you have a idea?
Should work OK, make sure you are getting the correct number for the stock in order to display the correct message.