Odyssee
Odyssee

Reputation: 2463

whmcs show active products per group

I'm trying to create 4 custom pages in whmcs, each for every productgroup there is. The first page, Web Hosting, has to include all the active web hosting packages for a user. I know that I have to set the variables myself, but I don't understand it fully

This gets the first domain in the table of the active services for a user.

$hostingProduct = Capsule::table('tblhosting')
      ->where('userid', '=', $ca->getUserID())->pluck('domain');

  $ca->assign('hostingProduct', $hostingProduct);

What I want to do now is get all the domains that are active for a certain product group. In this table is there no easy way to determine the product group. Howerver there is the packageid. Packageid 1, 2, 3 and 4 I can use for this page. I also need the id of the product from this table.

How do I get the right domains for those packageid's and the product id in an array? I want to call them like this in the .tpl file

{foreach $hostingProduct}
    <div class="col-md-3">
        <a href="clientarea.php?action=productdetails&id={$productid}">{$domain}</a>
    </div>
{/foreach}

Upvotes: 4

Views: 2668

Answers (2)

Odyssee
Odyssee

Reputation: 2463

Thanks to Luka Svalina for the tip :)

Because I needed the information from within the WHMCS installation, I used the internal API for this problem. The code from Luka Svaldina works, but this was I have more control about what is happening and what information I retrieve.

What I did was use the internal API Get Client Product as described in their documentation, the only downside of this method was that I couldn't set the gid for the products.

$command = "getclientsproducts";
$adminuser = "adminuser";
$values["clientid"] = $_SESSION['uid'];

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

$ca->assign('myVar', $results);

This returns the array for all the active products for that user. This isn't what I have been looking for, but it got me thinking about using it in my advantage. As the documentation states there you can use the pid to retrieve all the active products for that pid. So I used a getclientsproducts for every pid.

// webhosting product
// Basic Packages
$command = "getclientsproducts";
$adminuser = "adminuser";
$values["clientid"] = $_SESSION['uid'];
$values["pid"] = '19';

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

$ca->assign('basicVPS', $results);

// Standard Packages
$command = "getclientsproducts";
$adminuser = "adminuser";
$values["clientid"] = $_SESSION['uid'];
$values["pid"] = '20';

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

$ca->assign('standardVPS', $results);

// Pro Packages
$command = "getclientsproducts";
$adminuser = "adminuser";
$values["clientid"] = $_SESSION['uid'];
$values["pid"] = '21';

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

$ca->assign('proVPS', $results);

// Elite Packages
$command = "getclientsproducts";
$adminuser = "adminuser";
$values["clientid"] = $_SESSION['uid'];
$values["pid"] = '22';

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

$ca->assign('eliteVPS', $results);

This way every product I need for that page gets his own smarty variable that can be used. But again not exactly what I needed, so I used array_merge_recursive to merge the different arrays into one.

// webhosting product
// set API vars
$command = "getclientsproducts";
$adminuser = "adminuser";
$values["clientid"] = $_SESSION['uid'];

// pid
$values["pid"] = '2';
$results1 = localAPI($command,$values,$adminuser);
$values["pid"] = '5';
$results2 = localAPI($command,$values,$adminuser);
$values["pid"] = '7';
$results3 = localAPI($command,$values,$adminuser);
$values["pid"] = '1';
$results4 = localAPI($command,$values,$adminuser);

$results = array_merge_recursive($results1,$results2,$results3,$results4);

$ca->assign('webHosting', $results);

Exactly what I needed. Until it got me thinking about the creative use of array_merge_recursive in combination with the layout and bootstrap. I created two merged arrays, like this:

$a = array_merge_recursive($results1,$results3);
$b = array_merge_recursive($results2,$results4);

$ca->assign('webHosting_a', $a);
$ca->assign('webHosting_b', $b);

then I used foreach, odd and even to create a modular responsive grid with all the active products for that user. Something roughly like this:

    <div class="col-md-12">
        <div class="col-md-7>
        {foreach $webHosting_a.products.product as $product}
          {if $product@iteration is odd}
          <a href="/clientarea.php?action=productdetails&id={$product.id}">
          <div class="col-md-4">
            <div class="panel">
              <div class="panel-header">
                <h3><strong>{$product.domain}</strong></h3>
              </div>
              <div class="panel-content">
              {if $product.name eq 'Free'}
              <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {elseif $product.name eq 'Premium'}
                <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {elseif $product.name eq 'Professional'}
                <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {else}
                <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {/if}
              </div>
              <div class="panel-footer">
                <h3>{$product.name}</h3>
              </div>
            </div>
          </div>
          </a>
          {/if}
        {/foreach}
<div class="col-md-5">
        {foreach $webHosting_b.products.product as $product}
          {if $product@iteration is even}
          <a href="/clientarea.php?action=productdetails&id={$product.id}">
          <div class="col-md-4">
            <div class="panel">
              <div class="panel-header">
                <h3><strong>{$product.domain}</strong></h3>
              </div>
              <div class="panel-content">
              {if $product.name eq 'Free'}
              <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {elseif $product.name eq 'Premium'}
                <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {elseif $product.name eq 'Professional'}
                <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {else}
                <meter value="{$product.diskusage}" min=0 max="{$product.disklimit}" low=1 high=3>{$product.diskusage} of {$product.disklimit}</meter>
              {/if}
              </div>
              <div class="panel-footer">
                <h3>{$product.name}</h3>
              </div>
            </div>
          </div>
          </a>
          {/if}
        {/foreach}
        </div>
    </div>
</div>

And of course a bunch of if statements in case the user has only one product to make it all look good :)

Anyhow thanks for the tip Luka!!

Upvotes: 1

Luka Svalina
Luka Svalina

Reputation: 108

There is documentation on http://docs.whmcs.com/API:Get_Products code for external API to get all products in specific group is:

public function getproducts($gid) {
    App::uses('Xml', 'Utility');
    $url = $this -> geturl();
    $credentials = $this -> getcredentials();
    $username = $credentials['username'];
    $password = $credentials['password'];

    $postfields["username"] = $username;
    $postfields["password"] = md5($password);
    $postfields["action"] = "getproducts";
    $postfields["gid"] = $gid; //gid is group id

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    $data = curl_exec($ch);
    curl_close($ch);
    $array = Xml::toArray(Xml::build($data));
    return $array;
}

Hope this helps

Upvotes: 1

Related Questions