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/