When you create a client customfield, it's available in .tpl files in two different Smarty variables:

  • an array like this:
     customfields => array (18)
        0 => array(2)
              id =>83
              value => "mycustomvalue"
    1 => array(2)
          id =>87
          value => "mysecondcustomvalue"

    etc.
  • And a series of useless variables like:
        customfields1 = "mycustomvalue"
        customfields2 = "mysecondcustomvalue"



The first one does not allow any easy and compact way to get the value of a custom field, which can be found only through a loop:

{foreach from=$customfields item=field}
    {if $field.id == 17}
        {$field.value}
    {/if}
{/foreach}

but this is not efficient, and far from being error proof...

The second one instead is almost useless because there's no safe way to relate each smarty variable to a custom field; if you create a new custom field the order may change, and you'll have to edit all your custom tpl files...

An elegant solution is the following hook:

add_hook('ClientAreaPage', 1, function($vars) {    
    if (!empty($vars['clientsdetails']['customfields'])) {
        $customfieldsAssoc = [];
        foreach ($vars['clientsdetails']['customfields'] as $field) {
            $customfieldsAssoc[$field['id']] = $field['value'];
        }
        return ['customfieldsAssoc' => $customfieldsAssoc];

    }
});

This hook makes available the value of any client custom field in a template, using i.e. the form

{$customfieldsAssoc.17} 

(where "17" in the example is the id field in the tbl customfields)

 

Reference: https://whmcs.community/topic/348184-how-to-recover-client-customfields-values-in-smarty/

 

 

 

  • WHMCS, Smarty
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

 How to add a favicon in WHMCS

To add a favicon to your WHMCS install: copy your favicon (es. myfavicon.ico) in WHMCS folder...

 How to comment in template (.tpl) files

If you need to insert a comment in a .tpl file, or comment a block of code to disable it, you...

 How to insert a Youtube video in WHMCS knowledgebase article

To insert a YouTube video in an article of the WHMCS knowledge base, you need to insert following...

 How to disable a customer to pay invoices partly with credit balance

in WHMCS if a customer has not enough credit balance for full payment of an invoice, WHMCS allows...

 How to add a cookie bar to WHMCS

In order to add a cookie bar to your own installation of WHMCS: copy somewhere on your WHMCS...