Jump to content

custom client field - language variable


Sybille

Recommended Posts

Hi,

I generated in the WHMCS Admin tool a custom client field for title (Mr., Ms., etc.). This dropdown should appear in the registration form.

 

As well I would like to set a language variable for the value of the dropdown. I created for both values a text variable in my language File.

$_LANG['clientareatitle1'] = "Mr.";

$_LANG['clientareatitle2'] = "Ms.";

 

How do I include the variable in the WHMCS Admin tool, where I have to define the variable of my dropdown “title”?

 

Thanks in advanced for your help.

Custom_Client_Fields.png

Link to comment
Share on other sites

Hi Sybille,

 

I generated in the WHMCS Admin tool a custom client field for title (Mr., Ms., etc.). This dropdown should appear in the registration form.

then you'll need to tick the checkbox next to "Show On Order Form" - that will add it to both the order form and the registration page. :idea:

 

by default, custom client fields are shown at the bottom of the page... so if you want this dropdown closer to the Names fields, you'll probably want to move them (or just this one field).

 

if you only have this one client custom field, then you could edit the clientregister.tpl template and move the {if $customfields} block of code to where you want this 'Title' field to be shown...

 

                {if $customfields}
                   {foreach from=$customfields key=num item=customfield}
                       <div class="form-group">
                           <label class="control-label" for="customfield{$customfield.id}">{$customfield.name}</label>
                           <div class="control">
                               {$customfield.input} {$customfield.description}
                           </div>
                       </div>
                   {/foreach}
               {/if}

if you have many custom client fields, but only want to move the 'Title' field, then you'll need to use an {if} statement to identify the field to show.

 

http://forum.whmcs.com/showthread.php?73387-Placing-individual-custom-client-fields-on-the-registration-form&p=332402#post332402

 

I wouldn't necessarily copy any of the code in that thread directly, as it was written before v6, but the principle is still the same and still works... e.g., for v6, it should probably be something like this...

 

                {if $customfields}
                   {foreach from=$customfields key=num item=customfield}
                   {if $customfield.name eq "Title"}
                       <div class="form-group">
                            <label class="control-label"  for="customfield{$customfield.id}">{$customfield.name}</label>
                           <div class="control">
                               {$customfield.input} {$customfield.description}
                           </div>
                       </div>
                   {/if}
                   {/foreach}
               {/if}

you'd also have to do something similar to your order form registration/checkout templates too.

 

As well I would like to set a language variable for the value of the dropdown. I created for both values a text variable in my language File.

$_LANG['clientareatitle1'] = "Mr.";

$_LANG['clientareatitle2'] = "Ms.";

i'm not sure why you would need to do this?? :?:

 

if the client is logged in, then you should have access to this custom client field value by using {$clientsdetails.customfields1} or similar, e.g if you have multiple client fields, you may need to examine the Smarty arrays to see what its customfield value is.

Link to comment
Share on other sites

Hi Brian

 

Thanks a lot for your great help

 

i'm not sure why you would need to do this?? :?:

 

if the client is logged in, then you should have access to this custom client field value by using {$clientsdetails.customfields1} or similar, e.g if you have multiple client fields, you may need to examine the Smarty arrays to see what its customfield value is.

 

If the language is set to English the dropdown should say:

- Mr.

- Ms.

 

If the language is set to German the dropdown should say:

- Herr

- Frau

Link to comment
Share on other sites

you can do this it will be more easier for you to update, and keep your customization in future upgrades

 

create new PHP file inside /includes/hooks/ directory, customfieldtranslate.php for example, put the following code inside it:

 

<?php

add_hook("ClientAreaPage", 1, function($vars){

   $customfieldid = 20;

   foreach ($vars['customfields'] as $index => $customfield){

       if ($customfield['id']==$customfieldid){

           $newInput = str_replace(">Mr.<", ">".Lang::trans("clientareatitle1")."<", $customfield['input']);
           $newInput = str_replace(">Ms.<", ">".Lang::trans("clientareatitle2")."<", $newInput);

       }

       $vars['customfields'][$index]['input'] = $newInput;

   }

   return array("customfields" => $vars['customfields']);

});

 

in line #5 specify the custom filed ID

Link to comment
Share on other sites

If the language is set to English the dropdown should say:

- Mr.

- Ms.

 

If the language is set to German the dropdown should say:

- Herr

- Frau

oh I see, that makes sense now. :)

 

personally, if I was doing this, i'd slightly tweak sentq's hook and specifically your language variables to make them more logical...

 

first i'd change the dropdown to use Mr,Mrs,Ms etc - no '.' in the values...

 

then i'd alter the overrides to use these values...

 

$_LANG['Mr'] = "Herr."; 
$_LANG['Mrs'] = "Frau."; 
$_LANG['Ms'] = "Fräulein."; 

if you did that, then you could change the hook to be...

 

            $newInput = str_replace(">Mr<", ">".Lang::trans("Mr")."<", $customfield['input']);
           $newInput = str_replace(">Mrs<", ">".Lang::trans("Mrs")."<", $newInput);
           $newInput = str_replace(">Ms<", ">".Lang::trans("Ms")."<", $newInput);

on the register form side of things, this is only a minor change... where it becomes more useful is when you enter the client area...

 

if you wanted these custom fields multilingual in the client area, you could use the following in Smarty if you don't know the specific array value to use (if you do know, use it!)...

 

{foreach from=$clientsdetails.customfields key=num item=customfield}
                   {if $customfield.id eq "20"}
                   {if $LANG.{$customfield.value} neq ''}{$LANG.{$customfield.value}}{else}{$customfield.value}{/if}
                   {/if}
                   {/foreach}

but that's a rather long-winded way to do it - though depending on where you want to do this, if you need to edit the template, I tend to try and do everything either in the edit or in the hook - rather than doing some parts in the template, or some via a hook... but it would work either way. :idea:

 

it would possibly be simpler to create another CA hook to do a similar translation to sentq's previous hook.

 

if you were wanting to add this value to a navbar/sidebar/panel, then you'd certainly have to use a hook.

Link to comment
Share on other sites

Hi Brian,

 

Many thanks to your detailed instructions. :idea: But it didn't worked out. I guess I made one or a few mistakes... :?:

 

I created the hook customfieldtranslate.php (public_html/includes/hooks)

<?php

add_hook("ClientAreaPage", 1, function($vars){

   $customfieldid = 20;

   foreach ($vars['customfields'] as $index => $customfield){

       if ($customfield['id']==$customfieldid){

           $newInput = str_replace(">Mr<", ">".Lang::trans("Mr")."<", $customfield['input']);
           $newInput = str_replace(">Mrs<", ">".Lang::trans("Mrs")."<", $newInput);
       }

       $vars['customfields'][$index]['input'] = $newInput;

   }

   return array("customfields" => $vars['customfields']);

});

 

I added the translation to my language file (public_html/lang/overrides/german.php)

$_LANG['Mr'] = "Herr";
$_LANG['Mrs'] = "Frau";

 

 

Changed the clientregister.php

        <div class="row">			
           <div class="col-md-6">
               <div class="form-group">
		{foreach from=$clientsdetails.customfields key=num item=customfield}
					{if $customfield.id eq "Anrede"}
						{if $LANG.{$customfield.value} neq ''}{$LANG.{$customfield.value}}{else}{$customfield.value}{/if}
					{/if}
                   {/foreach}  
               </div>

               <div class="form-group">
                   <label for="firstname" class="control-label">{$LANG.clientareafirstname}</label>
                   <input type="text" name="firstname" id="firstname" value="{$clientfirstname}" class="form-control" {if !in_array('firstname', $optionalFields)}required{/if} />
               </div>

 

And changed the custom field in the admin tool

-> see print screen attachedWHMCS_Admin_CustomFields.pngWHMCS_Admin_CustomFields.png

 

Thanks once more for your help.

Link to comment
Share on other sites

one quick thing for you to check is the customfieldid value in the hook - as sentq suggested, if it's not '20' on your site, then it won't work - you'd need to find the correct value.

 

one way would be to go to register.php on your site, view the source in the browser and you should see something similar to below...

 

                                           <div class="form-group">
                           <label class="control-label" for="customfield3">Title</label>
                           <div class="control">
                               <select name="customfield[3]" id="customfield3" class="form-control"><option value="Mr">Mr</option><option value="Mrs">Mrs</option><option value="Miss">Miss</option></select> 
                           </div>
                       </div>

so for the hook to work on our dev site, I would change '20' to '3' (the value of the customfield) - if you can do the same on your site, hopefully that will be the issue.

 

if it helps, it's working on the dev using the value of 3 - but your value will almost certainly be different from 20.

Link to comment
Share on other sites

Hi Brian,

 

Thanks, the wrong ID was my mistake! Now I've got the correct one and it works correctly. :!:

 

Is it also possible to create a language variabe for the main title of the dropdown?

 

Extract from my language File:

[b]$_LANG['anredetitel'] = "Pers. Anrede";[/b]
$_LANG['anrede1'] = "Herr";
$_LANG['anrede2'] = "Frau";

 

Hook:

<?php

add_hook("ClientAreaPage", 1, function($vars){

   $customfieldid = 1;

   foreach ($vars['customfields'] as $index => $customfield){

       if ($customfield['id']==$customfieldid){
           [b]$newInput = str_replace(">anredetitel<", ">".Lang::trans("anredetitel")."<", $customfield['input']);[/b]
           $newInput = str_replace(">anrede1<", ">".Lang::trans("anrede1")."<", $customfield['input']);
           $newInput = str_replace(">anrede2<", ">".Lang::trans("anrede2")."<", $newInput);
       }
       $vars['customfields'][$index]['input'] = $newInput;
   }
   return array("customfields" => $vars['customfields']);
});

 

WHMCS Admin - Custom Field

WHMCS_Admin_CustomField.png

 

Form

Formular.png

 

Thanks again for your support

Link to comment
Share on other sites

Very usefull! It worked.

 

Sorry to bother you again but I tried to include the custom field to the user contacts (clientareacon-tacts.tpl, clientareaaddcontact.tpl) but it isn’t shown on the page.

 

I found this knowledgebase but it didn't solved my Problem.

 

<form role="form" method="post" ac-tion="{$smarty.server.PHP_SELF}?action=contacts&id={$contactid}">

<input type="hidden" name="submit" value="true" />

 

<div class="row">

<div class="col-md-6">

<div class="form-group">

{if $customfields}

{foreach from=$customfields key=num item=customfield}

<div class="form-group">

<label class="control-label" for="customfield{$customfield.id}">{$client_custom_field_anrede}</label>

<div class="control">

{$customfield.input} {$customfield.description}

</div>

</div>

{/foreach}

{/if}

</div>

 

<div class="form-group">

<label for="inputFirstName" class="control-label">{$LANG.clientareafirstname}</label>

<input type="text" name="firstname" id="inputFirstName" value="{$contactfirstname}" class="form-control" />

</div>

 

 

Do you know where in the code I got wrong?

Link to comment
Share on other sites

nothing wrong with the code, but the $customfields array isn't available to that template.

 

from Matt, the original author of WHMCS, five years ago...

 

http://forum.whmcs.com/showthread.php?37610-Missing-custom-fields-in-add-contacts

 

Custom fields are for clients not contacts so that won;t work. Custom fields aren't supported on contacts.

I don't think anything has changed since then.

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