Jump to content

Adding custom HOOK to display client account number


Recommended Posts

Hi WHMCS Community,

I am trying to display a WHMCS Client Account Number using a hook, I have the basic code as below:

<?php

use WHMCS\View\Menu\Item;
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAreaHomepagePanels', 1, function (Item $homePagePanels)
{
	$client = Menu::context( "client" );
	$fieldid = Capsule::table('tblcustomfields')->where('fieldname', 'Account Number')->where('type', 'client')->value('id');
	$fieldvalue = Capsule::table('tblcustomfieldsvalues')->where('fieldid', $fieldid)->where('relid', $client->id)->value('value'); 
	
	$bodyhtml = '<p>Your Account Number is '{$client_code}'.</p>';
	$examplePanel = $homePagePanels->addChild( 'Account Number', array(
           'label' => Lang::trans('Account Number'),
           'icon' => 'fa-example',
           'extras' => array(
               'color' => 'red',
               'btn-link' => '#',
               'btn-text' => Lang::trans('Account Number'),
               'btn-icon' => 'fa-plus',
           ),
           'bodyHtml' => $bodyhtml
           <p>{$client_code}</p>
       ));
});

However, nothing displays in the client area. Any help with this would be appreciated.

Kindest Regards, 

Nicholas Tae-Soo Sansom – Company Executive Officer
Root Layer Technologies, The Gardens, Unit 9A, 204 Alice Street, Brisbane, QLD 4000

 

Link to comment
Share on other sites

You had syntax error. Here's the fixed version.

<?php

use WHMCS\View\Menu\Item;
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAreaHomepagePanels', 1, function (Item $homePagePanels)
{
	$client = Menu::context( "client" );
	$fieldid = Capsule::table('tblcustomfields')->where('fieldname', 'Account Number')->where('type', 'client')->value('id');
	$fieldvalue = Capsule::table('tblcustomfieldsvalues')->where('fieldid', $fieldid)->where('relid', $client->id)->value('value');
	
	$bodyhtml = '<p>Your Account Number is ' . $client_code . '</p>';
	$examplePanel = $homePagePanels->addChild( 'Account Number', array(
			'label' => Lang::trans('Account Number'),
			'icon' => 'fa-example',
			'extras' => array(
					'color' => 'red',
					'btn-link' => '#',
					'btn-text' => Lang::trans('Account Number'),
					'btn-icon' => 'fa-plus',
			),
			'bodyHtml' => $bodyhtml . '<p>' . $client_code . '</p>',
			));
});

 

Edited by Kian
Link to comment
Share on other sites

Hi @Kian,

 

Thanks for that, it's perfect and now displays:

image.png.b2c2847d15ee25970b1d7f6467f368a1.png

However, it does not display the actual account number. Any ideas?
Just so you know it works from: https://infinitycds.co.za/whmcs-module2/whmcs-account-number/

Kindest Regards, 

Nicholas Tae-Soo Sansom – Company Executive Officer
Root Layer Technologies, The Gardens, Unit 9A, 204 Alice Street, Brisbane, QLD 4000

Link to comment
Share on other sites

Your Laravel query is not correct. The following one will work.

<?php

use WHMCS\View\Menu\Item;
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAreaHomepagePanels', 1, function (Item $homePagePanels)
{
	// Replace with the ID of your Client Custom Field
	$FieldID = '1';

	$Data = Capsule::select(Capsule::raw('SELECT t2.value FROM tblcustomfields AS t1 LEFT JOIN tblcustomfieldsvalues AS t2 ON t1.id = t2.fieldid WHERE t2.relid = "' . $_SESSION['uid'] . '" AND t1.id = "' . $FieldID . '"'));
	$FieldValue = $Data[0]->value;

	$bodyhtml = '<p>Your Account Number is ' . $FieldValue . '</p>';
	$examplePanel = $homePagePanels->addChild( 'Account Number', array(
			'label' => Lang::trans('Account Number'),
			'icon' => 'fa-example',
			'extras' => array(
					'color' => 'red',
					'btn-link' => '#',
					'btn-text' => Lang::trans('Account Number'),
					'btn-icon' => 'fa-plus',
			),
			'bodyHtml' => $bodyhtml . '<p>' . $FieldValue . '</p>',
			));
});

Notice that you need to change $FieldID with the ID of your Client Custom Field. You should avoid basing your query on field names. What if next year you decide to rename "Account Number" in as "Client Number"? IDs never change therefore use them.

Link to comment
Share on other sites

fundamentally, the Capsule query is fine - but the limitation that Kian mentions is perfectly true and that i'd also suggest using IDs...

the problem for you really centred that you were trying to output a variable that you hadn't defined... and was therefore empty... also, in recent versions, that FA icon wouldn't exist... nor can I particularly understand the reason for the + Account Number link top right if its not actually going to do anything...

<?php

use WHMCS\View\Menu\Item;
use WHMCS\Database\Capsule;

add_hook('ClientAreaHomepagePanels', 1, function (Item $homePagePanels)
{
	$client = Menu::context( "client" );
	$fieldid = Capsule::table('tblcustomfields')->where('fieldname', 'Account Number')->where('type', 'client')->value('id');
	$fieldvalue = Capsule::table('tblcustomfieldsvalues')->where('fieldid', $fieldid)->where('relid', $client->id)->value('value'); 
	$bodyhtml = '<p>Your Account Number is '.$fieldvalue.'.</p>';
	$examplePanel = $homePagePanels->addChild( 'Account Number', array(
           'label' => Lang::trans('Account Number'),
           'icon' => 'fa-user',
           'bodyHtml' => $bodyhtml
       ));
});
Link to comment
Share on other sites

Hi @brian! and @Kian,

Thanks, both for your input, I get what you are both saying and Brian, I was just using a default hook template to test my functionality before doing anything like styling etc.
I have tried the above code to make this work, however, I still seem to get to output of the Client Account Number. Am I doing something incorrectly here?

Link to comment
Share on other sites

4 minutes ago, RLT - Nicholas said:

I have tried the above code to make this work, however, I still seem to get to output of the Client Account Number. Am I doing something incorrectly here?

do you mean that it's not outputting the actual account number in the output ? are you sure that the value is stored as a Client Custom Field and not somewhere else in the database ?

Edited by brian!
Link to comment
Share on other sites

Just now, brian! said:

do you mean that it's not outputting the actual account number in the output ? are you sure that the value is stored as a Client Custom Field and not somewhere else in the database ?

Correct, I see no output in where I would expect to see the Account Number display. According to the the module "addon" developer, it should be viable anywhere within WHMCS.

Link to comment
Share on other sites

1 minute ago, RLT - Nicholas said:

Correct, I see no output in where I would expect to see the Account Number display. According to the the module "addon" developer, it should be viable anywhere within WHMCS.

if it's a Client Custom Field, then when you click on the Profile Tab in the Client Summary in the admin area, you should see the value ENGE02 (or whatever your equivalent Account Number value is) in one of the existing ustom fields... if it's not there, then it must be being stored elsewhere.

3oDZ5r6.png

Link to comment
Share on other sites

8 minutes ago, brian! said:

if it's a Client Custom Field, then when you click on the Profile Tab in the Client Summary in the admin area, you should see the value ENGE02 (or whatever your equivalent Account Number value is) in one of the existing ustom fields... if it's not there, then it must be being stored elsewhere.

3oDZ5r6.png

Hi @brian!,

You can see it's like this:

image.thumb.png.01a0c0816261532230c0d837f415d88b.png

However the field appears empty.

Link to comment
Share on other sites

8 minutes ago, RLT - Nicholas said:

However the field appears empty.

two thoughts...

  1.  did you install this account number addon *after* you had created this test account in WHMCS ? I don't know, but the implication from that module page you linked to, is that when a client account is created, a custom account number is created automatically... but if the test account was created *before* the addon was installed, then maybe the account number generation process hasn't occurred... You might need to go back to the developers to that, but frankly what I would do is manually enter a value into that "CLIENTCODE" customfield and see what happens.... or if you don't want to do that, find another client account that actually has a value in CLIENTCODE and use them to test the panel output.
  2. importantly - you're going to have to change that hook and tweak the reference of 'Account Number' to 'CLIENTCODE' in the code and see if doing both those things fix your situation.
Edited by brian!
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