Jump to content

Dynamically change website button when out of stock


JCHH

Recommended Posts

I am trying to figure out how to dynamically change the "Buy" button on my website to "Out of stock" when WHMCS reports 0 stock for a particular PID. I am, at the moment, using the following guide to pull the stock number from the WHMCS data feed. 

https://whmcs.community/topic/304174-display-stock-status-on-an-html-front-page

Now that only gets me the # of stock available, but I want to be able to have my website change the button text to "Out of stock" and disable the button if the stock shows "0". Could someone help me with this? Thanks in advanced!

Link to comment
Share on other sites

On 08/02/2021 at 05:33, JCHH said:

Now that only gets me the # of stock available, but I want to be able to have my website change the button text to "Out of stock" and disable the button if the stock shows "0". Could someone help me with this? Thanks in advanced!

what code are you using to display the buy now button on your site ?

as I see it, you have two main options - either use a conditional statement on your site when displaying the button based on the stock result that you get back from the feed, or get the feed itself to either generate a buy now or out of stock button based on the stock level.

Link to comment
Share on other sites

6 hours ago, brian! said:

what code are you using to display the buy now button on your site ?

as I see it, you have two main options - either use a conditional statement on your site when displaying the button based on the stock result that you get back from the feed, or get the feed itself to either generate a buy now or out of stock button based on the stock level.

Thank you for your response. Here is what's on my website as of now, they're just static buttons that I manually change every time there's a change in stock.

Out of stock button

<a href="#" class="btn btn-danger btn-xs disabled" role="button">Out Of Stock</a>

Buy Now button

<a href="whmcs_order_link" class="btn btn-danger btn-xs" role="button">Buy Now</a>

I'm not really an expert so I'm not sure how to proceed in making them dynamically change. Thank you in advanced!

Link to comment
Share on other sites

I think it'd be ideal for the feed generate the button based on the stock # of the PID, but I can't quite figure out how to do that. 

Right now, my stock.php feed is just the productsinfo.php but with this bit added in. How do I make an if condition to check the quantity and output a corresponding button depending on if the stock is >= 1?

elseif ($get=="qty") {
    widgetOutput($qty);

Thanks!

Link to comment
Share on other sites

Just another update-- I managed to set it to change the text of the button depending on if it's in stock or not, but I'm still not sure how I can disable the actual button so it can't be clicked if out of stock. I'm also not sure if this is the right way to do it but it works in terms of changing the text.

 

elseif ($get=="qty") {
    if($qty >=1) {
      widgetOutput('Buy Now');
    }
    else {
      widgetOutput('Out Of Stock');
    }
}

 

Link to comment
Share on other sites

Sorry another update... I'm stuck here for now but basically using the widgetOutput(''), I had it output a html button based on the same concept as above,

elseif ($get=="qty") {
      if($qty >=1) {
        widgetOutput('<a href="https://whmcs_url.com/cart.php?a=add&pid=11" class="btn btn-danger btn-xs" role="button">Deploy Now</a>');
      }
      else {
        widgetOutput('<a href="#" class="btn btn-danger btn-xs disabled" role="button">Out Of Stock</a>');
      }
}

Here's my issue though, I need the href link to be dynamic and I can't figure out how to do that. I tried using this, which didn't work. Changing around ' and " would cause the entire thing to stop working, so this is basically where I'm stuck on for now. Any additional help would be great, thank you!

elseif ($get=="qty") {
      if($qty >=1) {
        widgetOutput('<a href="{$systemUrl}cart.php?a=add&pid={$pid}" class="btn btn-danger btn-xs" role="button">Deploy Now</a>');
      }
      else {
        widgetOutput('<a href="#" class="btn btn-danger btn-xs disabled" role="button">Out Of Stock</a>');
      }
}

 

Link to comment
Share on other sites

  • 3 weeks later...
On 11/02/2021 at 07:43, JCHH said:

Here's my issue though, I need the href link to be dynamic and I can't figure out how to do that. I tried using this, which didn't work. Changing around ' and " would cause the entire thing to stop working, so this is basically where I'm stuck on for now. Any additional help would be great, thank you!

<?php

use WHMCS\Application;
use WHMCS\Config\Setting;
use WHMCS\Exception\ProgramExit;
use WHMCS\Product\Product;
use WHMCS\Session;
use WHMCS\User\Client;
use WHMCS\Database\Capsule;

require("../init.php");

/*
*** USAGE SAMPLES ***

<script language="javascript" src="feeds/stock.php?pid=1&get=name"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=description"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=qty"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=price&billingcycle=monthly&currency=1"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=orderurl&carttpl=web20cart"></script>

*/

$whmcs = App::self();
$pid = (int) $whmcs->get_req_var('pid');
$get = $whmcs->get_req_var('get');
$language = $whmcs->get_req_var('language') ?: null;
$qty = $whmcs->get_req_var('qty');
$data = array();
$name = $description = '';

// Verify user input for pid exists, is greater than 0, and as is a valid id
if ($pid > 0) {
    $data = Capsule::table('tblproducts')
        ->where('id', '=', $pid)
        ->first();
    $pid = (int) $data->id;
    // If there is a user logged in, we will use the client language
    if (((int) $userId = Session::get('userid'))) {
        $language = Client::find($userId, array('language'))->language ?: null;
    }
    $name = Product::getProductName($pid, $data->name, $language);
    $description = Product::getProductDescription($pid, $data->description, $language);
}

// Verify that the pid is not less than 1 to in order to continue.
if ($pid < 1) {
    widgetOutput('Product ID Not Found');
}

if ($get=="name") {
    widgetOutput($name);
} elseif ($get=="description") {
    $description = str_replace(array("\r", "\n", "\r\n"), "", nl2br($description));
    widgetOutput($description);
} elseif ($get=="configoption") {
    $configOptionNum = $whmcs->get_req_var('configoptionnum');
    if (!$configOptionNum) {
        widgetOutput('The variable configoptionnum is required when get is configoption.');
    }
    widgetoutput($data['configoption' . (int) $configOptionNum]);
} elseif ($get=="orderurl") {
    $cartTemplate = $whmcs->get_req_var('carttpl');
    if ($cartTemplate == "ajax") {
        $cartTemplate = "ajaxcart";
    }
    $systemUrl = App::getSystemUrl();
    if (!$cartTemplate) {
        $cartTemplate = Setting::getValue('OrderFormTemplate ');
    }
    widgetOutput("{$systemUrl}cart.php?a=add&pid={$pid}&carttpl={$cartTemplate}");
} elseif ($get=="price") {
    // Verify user input for currency exists, is numeric, and as is a valid id
    $billingCycle = $whmcs->get_req_var('billingcycle');
    $currencyID = $whmcs->get_req_var('currency');
    if (!is_numeric($currencyID)) {
        $currency = array();
    } else {
        $currency = getCurrency('', $currencyID);
    }

    if (!$currency || !is_array($currency) || !isset($currency['id'])) {
        $currency = getCurrency();
    }
    $currencyID = $currency['id'];

    $data = Capsule::table('tblpricing')
        ->where('type', '=', 'product')
        ->where('currency', '=', $currencyID)
        ->where('relid', '=', $pid)
        ->first();
    $price = $data->$billingCycle;
    $price = formatCurrency($price);
    widgetOutput($price);
} elseif ($get=="qty") {
	$product = Product::find($pid);
	$stockqty = $product->quantityInStock;
	$stockcontrol = $product->stockControlEnabled;
	if($stockcontrol && $stockqty > 0) {
		$systemurl = Setting::getValue('SystemURL');
		widgetOutput('<a href="'.$systemurl.'/cart.php?a=add&pid='.$pid.'" class="btn btn-danger btn-xs" role="button">Deploy Now</a>');
	} else {
		widgetOutput('<a href="#" class="btn btn-danger btn-xs disabled" role="button">Out Of Stock</a>');
    }
} else {
    widgetOutput('Invalid get option. Valid options are "name", "description", "configoption", "orderurl", "price" or "qty"');
}

/**
 * The function to output the widget data to the browser in a javascript format.
 *
 * @throws WHMCS\Exception\ProgramExit
 * @param string $value the data to output
 */
function widgetOutput($value) {
    echo "document.write('".addslashes($value)."');";
    throw new ProgramExit();
}

in the the feed link, you would just need to pass a pid value and get the qty value...

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

it checks whether stock control is enabled on the given product, and if the qty is greater than 0 - if both true, it will add the deploy button - otherwise, it shows the disabled out of stock button.

Link to comment
Share on other sites

17 hours ago, brian! said:

<?php

use WHMCS\Application;
use WHMCS\Config\Setting;
use WHMCS\Exception\ProgramExit;
use WHMCS\Product\Product;
use WHMCS\Session;
use WHMCS\User\Client;
use WHMCS\Database\Capsule;

require("../init.php");

/*
*** USAGE SAMPLES ***

<script language="javascript" src="feeds/stock.php?pid=1&get=name"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=description"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=qty"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=price&billingcycle=monthly&currency=1"></script>
<script language="javascript" src="feeds/stock.php?pid=1&get=orderurl&carttpl=web20cart"></script>

*/

$whmcs = App::self();
$pid = (int) $whmcs->get_req_var('pid');
$get = $whmcs->get_req_var('get');
$language = $whmcs->get_req_var('language') ?: null;
$qty = $whmcs->get_req_var('qty');
$data = array();
$name = $description = '';

// Verify user input for pid exists, is greater than 0, and as is a valid id
if ($pid > 0) {
    $data = Capsule::table('tblproducts')
        ->where('id', '=', $pid)
        ->first();
    $pid = (int) $data->id;
    // If there is a user logged in, we will use the client language
    if (((int) $userId = Session::get('userid'))) {
        $language = Client::find($userId, array('language'))->language ?: null;
    }
    $name = Product::getProductName($pid, $data->name, $language);
    $description = Product::getProductDescription($pid, $data->description, $language);
}

// Verify that the pid is not less than 1 to in order to continue.
if ($pid < 1) {
    widgetOutput('Product ID Not Found');
}

if ($get=="name") {
    widgetOutput($name);
} elseif ($get=="description") {
    $description = str_replace(array("\r", "\n", "\r\n"), "", nl2br($description));
    widgetOutput($description);
} elseif ($get=="configoption") {
    $configOptionNum = $whmcs->get_req_var('configoptionnum');
    if (!$configOptionNum) {
        widgetOutput('The variable configoptionnum is required when get is configoption.');
    }
    widgetoutput($data['configoption' . (int) $configOptionNum]);
} elseif ($get=="orderurl") {
    $cartTemplate = $whmcs->get_req_var('carttpl');
    if ($cartTemplate == "ajax") {
        $cartTemplate = "ajaxcart";
    }
    $systemUrl = App::getSystemUrl();
    if (!$cartTemplate) {
        $cartTemplate = Setting::getValue('OrderFormTemplate ');
    }
    widgetOutput("{$systemUrl}cart.php?a=add&pid={$pid}&carttpl={$cartTemplate}");
} elseif ($get=="price") {
    // Verify user input for currency exists, is numeric, and as is a valid id
    $billingCycle = $whmcs->get_req_var('billingcycle');
    $currencyID = $whmcs->get_req_var('currency');
    if (!is_numeric($currencyID)) {
        $currency = array();
    } else {
        $currency = getCurrency('', $currencyID);
    }

    if (!$currency || !is_array($currency) || !isset($currency['id'])) {
        $currency = getCurrency();
    }
    $currencyID = $currency['id'];

    $data = Capsule::table('tblpricing')
        ->where('type', '=', 'product')
        ->where('currency', '=', $currencyID)
        ->where('relid', '=', $pid)
        ->first();
    $price = $data->$billingCycle;
    $price = formatCurrency($price);
    widgetOutput($price);
} elseif ($get=="qty") {
	$product = Product::find($pid);
	$stockqty = $product->quantityInStock;
	$stockcontrol = $product->stockControlEnabled;
	if($stockcontrol && $stockqty > 0) {
		$systemurl = Setting::getValue('SystemURL');
		widgetOutput('<a href="'.$systemurl.'/cart.php?a=add&pid='.$pid.'" class="btn btn-danger btn-xs" role="button">Deploy Now</a>');
	} else {
		widgetOutput('<a href="#" class="btn btn-danger btn-xs disabled" role="button">Out Of Stock</a>');
    }
} else {
    widgetOutput('Invalid get option. Valid options are "name", "description", "configoption", "orderurl", "price" or "qty"');
}

/**
 * The function to output the widget data to the browser in a javascript format.
 *
 * @throws WHMCS\Exception\ProgramExit
 * @param string $value the data to output
 */
function widgetOutput($value) {
    echo "document.write('".addslashes($value)."');";
    throw new ProgramExit();
}

in the the feed link, you would just need to pass a pid value and get the qty value...


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

it checks whether stock control is enabled on the given product, and if the qty is greater than 0 - if both true, it will add the deploy button - otherwise, it shows the disabled out of stock button.

Thank you so much!

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.

Guest
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