Jump to content

API GetProducts - Get the hidden field in products (SOLUTION)


Recommended Posts

while working with the api I realized that getproducts (https://developers.whmcs.com/api-reference/getproducts/) does not bring up the field to know if the product is active or not.

I saw someone post a similar idea several years ago: 

 

so with a little engineer I made my own version of getproducts and uploaded it to /includes/api/

Request Parameters "GetProductsActive"

Parameter Type Description Required
action string “GetProductsActive” Required
pid int string Obtain a specific product id configuration. Can be a list of ids comma separated
gid int Retrieve products in a specific group id Optional
       

Response Parameters

Parameter Type Description
result string The result of the operation: success or error
totalresults int The total number of results available
startnumber int The starting number for the returned results
numreturned int The number of results returned
products array An array of products matching the criteria passed

I will leave the file called: getproductsactive.php

however this may throw an api error, you have to give access in the table tblapi_roles in database, in the field: permissions add: ,"getproductsactive": 1 before closing }, This modification is due to WHMCS not showing the files added in includes/api as a role. (I do not know why).

With this you can get the field: hidden which defines 0 if it is visible and 1 if it is hidden. (true or false also works)

my code is a copy of includes/api/getproduct.php just add to show the hidden field (, "hidden" => $data["hidden"]), this would have been easier if WHMCS implemented it, it only took me 2 minutes to show this field.

 

<?php
if (!defined("WHMCS")) {
    exit("This file cannot be accessed directly");
}
if (!function_exists("getCustomFields")) {
    require ROOTDIR . "/includes/customfieldfunctions.php";
}
if (!function_exists("getCartConfigOptions")) {
    require ROOTDIR . "/includes/configoptionsfunctions.php";
}
global $currency;
$currency = getCurrency();
$pid = $whmcs->get_req_var("pid");
$gid = $whmcs->get_req_var("gid");
$module = $whmcs->get_req_var("module");
$where = array();
if ($pid) {
    if (is_numeric($pid)) {
        $where[] = "tblproducts.id=" . (int) $pid;
    } else {
        $pids = array();
        foreach (explode(",", $pid) as $p) {
            $p = (int) trim($p);
            if ($p) {
                $pids[] = $p;
            }
        }
        if ($pids) {
            $where[] = "tblproducts.id IN (" . implode(",", $pids) . ")";
        }
    }
}
if ($gid) {
    $where[] = "gid=" . (int) $gid;
}
if ($module && preg_match("/^[a-zA-Z0-9_\\.\\-]*\$/", $module)) {
    $where[] = "servertype='" . db_escape_string($module) . "'";
}
$result = select_query("tblproducts", "tblproducts.*", implode(" AND ", $where), "tblproductgroups`.`order` ASC, `tblproductgroups`.`id` ASC, `tblproducts`.`order` ASC, `tblproducts`.`id", "ASC", "", "tblproductgroups ON tblproducts.gid = tblproductgroups.id");
$apiresults = array("result" => "success", "totalresults" => mysql_num_rows($result));
while ($data = mysql_fetch_array($result)) {
    $pid = $data["id"];
    $productarray = array("pid" => $data["id"], "gid" => $data["gid"], "type" => $data["type"], "name" => $data["name"], "description" => $data["description"], "module" => $data["servertype"], "paytype" => $data["paytype"], "hidden" => $data["hidden"]);
    if ($language = $whmcs->get_req_var("language")) {
        $productarray["translated_name"] = WHMCS\Product\Product::getProductName($data["id"], $data["name"], $language);
        $productarray["translated_description"] = WHMCS\Product\Product::getProductDescription($data["id"], $data["description"], $language);
    }
    if ($data["stockcontrol"]) {
        $productarray["stockcontrol"] = "true";
        $productarray["stocklevel"] = $data["qty"];
    }
    $result2 = select_query("tblpricing", "tblcurrencies.code,tblcurrencies.prefix,tblcurrencies.suffix,tblpricing.msetupfee,tblpricing.qsetupfee,tblpricing.ssetupfee,tblpricing.asetupfee,tblpricing.bsetupfee,tblpricing.tsetupfee,tblpricing.monthly,tblpricing.quarterly,tblpricing.semiannually,tblpricing.annually,tblpricing.biennially,tblpricing.triennially", array("type" => "product", "relid" => $pid), "code", "ASC", "", "tblcurrencies ON tblcurrencies.id=tblpricing.currency");
    while ($data = mysql_fetch_assoc($result2)) {
        $code = $data["code"];
        unset($data["code"]);
        $productarray["pricing"][$code] = $data;
    }
    $customfieldsdata = array();
    $customfields = getCustomFields("product", $pid, "", "", "on");
    foreach ($customfields as $field) {
        $customfieldsdata[] = array("id" => $field["id"], "name" => $field["name"], "description" => $field["description"], "required" => $field["required"]);
    }
    $productarray["customfields"]["customfield"] = $customfieldsdata;
    $configoptiondata = array();
    $configurableoptions = getCartConfigOptions($pid, array(), "", "", "", true);
    foreach ($configurableoptions as $option) {
        $options = array();
        foreach ($option["options"] as $op) {
            $pricing = array();
            $result4 = select_query("tblpricing", "code,msetupfee,qsetupfee,ssetupfee,asetupfee,bsetupfee,tsetupfee,monthly,quarterly,semiannually,annually,biennially,triennially", array("type" => "configoptions", "relid" => $op["id"]), "", "", "", "tblcurrencies ON tblcurrencies.id=tblpricing.currency");
            while ($oppricing = mysql_fetch_assoc($result4)) {
                $currcode = $oppricing["code"];
                unset($oppricing["code"]);
                $pricing[$currcode] = $oppricing;
            }
            $options["option"][] = array("id" => $op["id"], "name" => $op["name"], "rawName" => $op["rawName"], "recurring" => $op["recurring"], "required" => $op["required"], "pricing" => $pricing);
        }
        $configoptiondata[] = array("id" => $option["id"], "name" => $option["optionname"], "type" => $option["optiontype"], "options" => $options);
    }
    $productarray["configoptions"]["configoption"] = $configoptiondata;
    $apiresults["products"]["product"][] = $productarray;
}
$responsetype = "xml";

?>

Hope this helps others to show the field they need to show files and hidden products.

 

getproductsactive.php

Edited by JesusSuarz
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.

×
×
  • 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