Jump to content

How to add custom client field in .tpl templates


Inna

Recommended Posts

Hello,

I added a new custom client field for phone number (I use 3rd party addon which requires new custom field), and my problem is I cannot add that field into the registration page and client area page.

I googled a lot regarding how to do that, but none of them unfortunately worked.

I use WHMCS v8 with custom theme, but it neither worked with default theme.

When I refer to ` tblcustomfields` table, I see this field has `relid` equals with 0 and `id` is 32, and now when I see `tblcustomfieldsvalues`, that phone number of the test client has `fieldid` equals to 32 and `relid` is 1 which is I think client number and it's correct if my assumption is right.

The I tried and failed is that the new code I add to template is not shown at all.

I should mention that I added {debug} to that template and this works and shows debug info, but when I want to show that field in these two pages, I'm unable to do that.

 

Would you please help me how may I reach it?

Link to comment
Share on other sites

1 hour ago, Inna said:

Would you please help me how may I reach it?

have you ensured that the custom field has its "Show On Order Form" checkbox ticked ?

https://docs.whmcs.com/Custom_Fields#Client_Custom_Fields

Quote

You can create client profile-related fields in Configuration () > System Settings > Custom Client Fields. Set these to Show on Order Form if you want the client to enter them. You can also leave them as only visible in the admin area, for private entries. The client can view and edit the field values from the client area unless you set them to admin only.

Link to comment
Share on other sites

Hello,

 

Regarding Show On Order Form, I thought it will show on Order Form, not Registration page:) This is now fixed:)

 

But I'm still unable to show the field on client area page.

My field page is not English, it's for example 'تلفن همراه' which means phone number.

So as per the docs, I should write {$service_custom_field_تلفنهمراه} to show in the pages, am I right?

Link to comment
Share on other sites

35 minutes ago, Inna said:

So as per the docs, I should write {$service_custom_field_تلفنهمراه} to show in the pages, am I right?

that should only apply to the email templates (products), not the client area.

if you purely wanted to show the value of the customfield, then it would be available to the $clientsdetails array when a user is logged in

vjxxvWp.png

so in the above example, {$clientsdetails.customfields21} would show the value...

1pktcnY.png

its position in that array will be defined by its "Display Order" value - though it's relative position can change if you add other client fields.

if you needed to use a hook to query the database for it's value, then you could use...

<?php

# Client Custom Field Hook
# Written by brian!

use WHMCS\Database\Capsule;

function client_custom_field_phone_hook($vars) {
	
	$client = Menu::context('client');
	$fieldid = 92;
	if (!is_null($client)) {
		$cfvalue = Capsule::table('tblcustomfieldsvalues')->where([['fieldid',$fieldid],['relid',$client->id]])->value('value');
		return array("customfieldphone" => $cfvalue);
	}
}
add_hook("ClientAreaPage", 1, "client_custom_field_phone_hook");

and that would allow you to use {$customdieldphone} in your template to display it's value - the only line that you should need to change is its fieldid value and that will be the ID value of this customfield in tblcustomfields - in the above example, I can see its value is 92 from the {debug} screenshot, but you could equally find the value from looking in tblcustomfields if you're familiar with that now.

also, it's worth mentioning that the above hook would run on every client page - if you only need it to run on a specific page, then you would either have to limit the hook to check the template and only run on that specific page (that's a simple if statement), of you may be able to use a more specific hook point rather than ClientAreaPage (but that will depend on which template you're trying to output this text).

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