Jump to content

New API function to get Product addons


Recommended Posts

Create a file in whmcsfolder/includes/api/. Label it in the name of the API call getproductaddons.php, all lowercase.

 

Paste in the code (feel free to contirbute):

<?php

if (!defined("WHMCS")) {
   die("This file cannot be accessed directly");
}

function getParams($vars) {
   $param = array('action' => array(), 'params' => array());
   if (isset($vars['cmd'])) {
//Local API mode
       $param['action'] = $vars['cmd'];
       $param['params'] = (object) $vars['apivalues1'];
       $param['adminuser'] = $vars['adminuser'];
   } else {
//Post CURL mode
       $param['action'] = $vars['_POST']['action'];
       unset($vars['_POST']['username']);
       unset($vars['_POST']['password']);
       unset($vars['_POST']['action']);
       $param['params'] = (object) $vars['_POST'];
   }
   return (object) $param;
}

try {
// Get the arguments
   $vars = get_defined_vars();
   $postFields = getParams($vars);

// Block of code to update the whmcs details
   $pkg = addons($pid);
// Block ends here

   $apiresults = array("result" => "success", "addons" => $pkg);
} catch (Exception $e) {
   $apiresults = array("result" => "error", "message" => $e->getMessage());
}

function addons($pid) {

   $pkg = array();
   $pkgIndex = 0;

   $table = "tbladdons";
   $fields = "*";
   $where = array("showorder" => 'on');

   $result = select_query($table, $fields, $where);

   while ($row = mysql_fetch_assoc($result)) {
       if ($row['packages'] != '') {

           $packages = explode(",", $row['packages']);

           foreach ($packages as $package) {
               if ($package == $pid) {
                   $pkg[$pkgIndex]['id'] = $row['id'];
                   $pkg[$pkgIndex]['name'] = $row['name'];
                   $pkg[$pkgIndex]['description'] = $row['description'];

                   if ($row['billingcycle'] == "Free Account") {
                       $pkg[$pkgIndex]['cost'] = 0;
                   } else {
                       $pkg[$pkgIndex]['cost'] = prodPrice($row['id'], "monthly", 'addon'); // addon prices allways in 'monthly' field, independently of Billing Cycle.
                   }

                   $pkg[$pkgIndex]['cycle'] = $row['billingcycle'];
                   $pkgIndex++;
               }
           }
       }
   }

   return $pkg;
}

function prodPrice($pid, $cycle = NULL, $type = 'product', $currency = '1') {
   $table = "tblpricing";
   $fields = "monthly,quarterly,semiannually,annually,biennially";
   $where = array("type" => $type, "currency" => $currency, "relid" => $pid);
   $limits = "1";

   $result = select_query($table, $fields, $where, '', '', $limits);
   while ($addon = mysql_fetch_assoc($result)) {
       return $addon[$cycle];
   }
}

 

Save file, and now you can use it:

$command = "getproductaddons";
$adminuser = "admin";
$values["pid"] = 5;

$results = localAPI($command,$values,$adminuser);

 

All the best!

 

Please give support!

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 years later...

 

On 4/11/2015 at 10:22 PM, Alex - Arvixe said:

Whilst I can see how this could be useful, why wouldn't it be possible to use: http://docs.whmcs.com/API:Get_Clients_Addons

 

Filtering it down by adding a service ID?

I know this is an old post but this wont work because it will retrieve only the services added to the first client. If you want all available addons for a particular service, that doesnt seem available via the api

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