Jump to content
Sign in to follow this  
BlueAngelHost

Display Stock Status on an HTML front page

Recommended Posts

1 hour ago, BlueAngelHost said:

I check data feeds option but I cannot find any option to display the stock status on my html front website.

it would have to be custom written - though it should literally only take a few lines of code... if I quickly modify the productsinfo.php feed to have a stock option, then the code would look like...

<?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/productsinfo.php?pid=1&get=name"></script>

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

<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly&currency=1"></script>

<script language="javascript" src="feeds/productsinfo.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;
$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;
    $qty = (int) $data->qty;
    // 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") {
    widgetOutput($qty);	
} else {
    widgetOutput('Invalid get option. Valid options are "name", "description", "configoption", "orderurl", "qty" or "price"');
}

/**
 * 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();
}

however, do not modify the existing productsinfo.php file as it will get overwritten during an update... create a new .php file in the feeds folder, call it productstock.php (or whatever) and then to display a stock quantity for a given product, you would call the feed using...

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

the feed will return the stock quantity for the product, but will not contain any styling (unlike your image) - you can add your styling to the template or the feed, whichever you're more comfortable doing.

Share this post


Link to post
Share on other sites
5 hours ago, brian! said:

it would have to be custom written - though it should literally only take a few lines of code... if I quickly modify the productsinfo.php feed to have a stock option, then the code would look like...


<?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/productsinfo.php?pid=1&get=name"></script>

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

<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly&currency=1"></script>

<script language="javascript" src="feeds/productsinfo.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;
$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;
    $qty = (int) $data->qty;
    // 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") {
    widgetOutput($qty);	
} else {
    widgetOutput('Invalid get option. Valid options are "name", "description", "configoption", "orderurl", "qty" or "price"');
}

/**
 * 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();
}

however, do not modify the existing productsinfo.php file as it will get overwritten during an update... create a new .php file in the feeds folder, call it productstock.php (or whatever) and then to display a stock quantity for a given product, you would call the feed using...


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

the feed will return the stock quantity for the product, but will not contain any styling (unlike your image) - you can add your styling to the template or the feed, whichever you're more comfortable doing.

Is there a way to do the same but with the price of a product with X currency?

Share this post


Link to post
Share on other sites
15 hours ago, Eldremor said:

Is there a way to do the same but with the price of a product with X currency?

if you just wanted to show a product price using a feed, then you can already do that...

<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly&currency=1"></script>

or are you looking to return both a product price and it's stock quantity at the same time to be used together?

Share this post


Link to post
Share on other sites
On 12/22/2020 at 7:10 PM, brian! said:

<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly&currency=1"></script>

Hi @brian!

How can I change currency based on country in it?

Edited by HEROCLOUDS

Share this post


Link to post
Share on other sites
1 hour ago, brian! said:

are you thinking of a free geolocation solution such as ModulesGarden's ?

It will change currency automatically when we using 

 

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

 

??

 

 

Thanks 

Share this post


Link to post
Share on other sites
23 hours ago, ManagedCloud-Hosting said:

I have two currencies one my Country currency and another is USD. Will this Geolocation Hook For WHMCS work fine for me ?

if you wanted new users from your country to default to that country's currency and other users to be shown USD prices, then it should work for that (assuming the users weren't using a VPN) - obviously existing users wouldn't be affected as they can't change their currencies.

Share this post


Link to post
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.

Sign in to follow this  

  • 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