Jump to content

Out of stock message in template file


Recommended Posts

Hi all,


We have our products listed in tables.

At the right site we have the Configure link.

We want to have in automated way to pull out instead the configuration link a "out of stock" message if the stock is 0. If not, customer will see the configuration link.


We use the following code in the tpl files, but it works only with pid ID 1. If we change the pid IP, it pulls always out of stock. Here the code with the pid ID 118:






mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());

mysql_select_db($db_name) or die(mysql_error());


$query = "SELECT qty FROM tblproducts WHERE id='118'";


$data = mysql_fetch_array($result);

$qty = $data['qty'];

if ($qty=="0") {

echo("Out Of Stock");




echo'<a href="https://www.xxxxxxx.com/cart.php?a=add&pid=118"><img src="templates/new/html/configure.png" alt="" width="70" height="15"></a>';






Can some one help me out?



Edited by webpoint
Link to comment
Share on other sites

as using {php} in templates is going to be deprecated when WHMCS starts using Smarty v3 (due in the v6 release I believe), it is probably good to get out of the habit of adding {php} code to the templates...


i'm not sure how you're creating your tables, but one way to get the output you require, in terms of stock, would be to use a data feed - http://docs.whmcs.com/Data_Feeds


that will allow you to keep the php out of the templates and just call it when required via a javascript call..


so add the following code to your .tpl template where you want to have an out of stock/configure link to the product, changing the PID number to the correct one for the product.


<script language="javascript" src="feeds/stock.php?pid=45"></script>


and then create a file in your feeds directory, call it stock.php and add the following code to it...





<script language="javascript" src="feeds/stock.php?pid=1"></script>

   $whmcs = WHMCS_Application::getInstance();
   $pid = $whmcs->get_req_var('pid');

   // Verify user input for pid exists, is numeric, and as is a valid id
   if (is_numeric($pid)) {
       $result = select_query("tblproducts", "", array("id" => $pid));
       $data = mysql_fetch_array($result);
       $pid = $data['id'];
       $stockcontrol = $data['stockcontrol'];
       $qty = $data['qty'];
   } else {
       $pid = '';

   if ($stockcontrol=="on" && $qty==0)  {

       $output = $_LANG['outofstock'];

   else  {

       $output = '<a href="https://www.xxxxxxx.com/cart.php?a=add&pid='.$pid.'"><img src="templates/new/html/configure.png" alt="" width="70" height="15"></a>';


function widgetoutput($value) {
   echo "document.write('".addslashes($value)."');";


i've tweaked your idea slightly to check that stockcontrol is enabled on the product - if it isn't enabled, the qty is 0 by default... this may not be necessary if you know for sure that all your products have stockcontrol enabled, but I thought it worth including the additional check just in case there are products that are not.


so if stockcontrol is enabled and qty equals 0, we display an "Out of Stock" message in the language of the user, otherwise it creates a link as per your own code and automatically adds the PID value to the URL.


of course for consistency, you could replace the OoS message with an image the same size as the configure button.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated