Jump to content

how to recover client customfields values in Smarty?


Go to solution Solved by Remitur,

Recommended Posts

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"

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

The first one should be more usefull, because the "id" is the same id of the customfield in  the tbl customfields in the database

 So:

  •  you create a new client customfield in admin area
  • go to tbl customfields, and recover the id field of the new customfield   (i.e. 17)

And you should be able to recover its value in tpl, because it will be somewhere in the array "customfields", as "value" element of the sub-array which has id=17

Well, this is the point: how can I recover using Smarty the "value" element of the sub-array with id=17???????????????? 

 

 

 

Link to comment
Share on other sites

Update: the only way I found to do it is extremely inefficient and almost stupid, through a loop:

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

Any better idea?

Link to comment
Share on other sites

  • Solution

Thanks to @Mytihost, I found an elegant solution for this issue.

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];

    }
});

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

{$customfieldsAssoc.17} 

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

Edited by Remitur
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