About Club osCommerce

Showcasing osCommerce…the good, the bad and the ugly!

StumbleUpon It! DIGG It!

Show Stock Availability in osCommerce Product Info

Written By Gary on Jan 26 2009 · Comments (7)

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:

PHP:
  1. <?php echo tep_get_products_stock($product_info['products_id']); ?>

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:
  1. function clubosc_products_stock($pID) {
  2.       switch(tep_get_products_stock($pID)) {
  3.           case 10:
  4.           case 9:
  5.           case 8:
  6.           case 7:
  7.           case 6:
  8.           $in_stock = TEXT_LIMITED_AVAILABILITY;
  9.           break;
  10.           case 5:
  11.           case 4:
  12.           case 3:
  13.           case 3:
  14.           $in_stock = TEXT_VERY_LIMITED_AVAILABILITY;
  15.           break;
  16.           case 1:
  17.           $in_stock = TEXT_ULTRA_LIMITED_AVAILABILITY;
  18.           break;
  19.           default:
  20.           $in_stock = TEXT_UNLIMITED_AVAILABILITY;
  21.       }
  22.       return $in_stock;
  23.   }

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:
  1. define('TEXT_LIMITED_AVAILABILITY', 'We have a few in stock...');
  2. define('TEXT_VERY_LIMITED_AVAILABILITY', 'We have very few in stock...');
  3. define('TEXT_ULTRA_LIMITED_AVAILABILITY', 'Only 1 left, buy it now!');
  4. define('TEXT_UNLIMITED_AVAILABILITY', 'We have loads in stock...');

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:
  1. <?php echo clubosc_products_stock($product_info['products_id']); ?>

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:
  1. case 15:
  2.           case 14:
  3.           case 13:
  4.           case 12:
  5.           case 11:
  6.           $in_stock = TEXT_SORTOF_LIMITED_AVAILABILITY;
  7.           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:

PHP:
  1. $in_stock = tep_image(DIR_WS_IMAGES . 'loads.gif');

Have fun!

7 Comments

  1. [...] 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


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


  3. case "-13":

    might work, I haven't tested it.

    Comment by Gary — November 10, 2009 @ 1:02 pm


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


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


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


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


Leave a comment

RSS feed for comments on this post · TrackBack URL

Hot 100 osCommerce Shops

View the osCommerce HOT 100These are the best looking, most exceptional osCommerce Stores as voted for by you.

New to osCommerce - get inspiration from these beautiful shops. Reckon your site has what it takes to become a member of the HOT 100? Submit it!