Show Stock Availability in osCommerce Product Info

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!

13 Replies to “Show Stock Availability in osCommerce Product Info”

  1. 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?

  2. You could use the default for anything below 1. Experiment, read the PHP page about “switch” function…learn from your mistakes.

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

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

  5. 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?

  6. Should work OK, make sure you are getting the correct number for the stock in order to display the correct message.

Leave a Reply

Your email address will not be published. Required fields are marked *