Whmcs Client Custom Field Value

Asked by At

Try my luck here after 1 week of no sucessfull help from WHMCS forum, and support only refering to Docs and Forum.

I wish to get value from fields from Client Custom fields, where I know the fieldname

Per documentation and forum post this should in theory work, but it doesnt, nothing is returned. Any idea?

    $clientFields =  Client::find($clientID)->customFieldValues  ;  
foreach($clientFields AS $field)
{

if (isset($field->customField->fieldName) 

and $field->customField->fieldName == 'NoridCID') 

{ 
    $xml .= $field->customField->value; // Add ID to XML 
}   

    else
    {
        return array("error" => "Some Error Message");

    }
}

1 Answers

0
Community On

This should really be very easy. I'm going to show you the different ways of doing this depending on what you want you want to retrieve and the information you have at hand. Of course I know what you know is the fieldname, though I would recommend getting into your tblcustomfields table of your WHMCS installation and retrieve the id value for that custom field. Now let's get down to the code:

First: Include the following code:

require("init.php");
use WHMCS\Database\Capsule;

Second: Declare the known variable:

I am assuming you already have the values you know. So I'm just going to use my pseudo values here:

$clientID = 12;
$customFieldID = 1;
$customFieldName = 'Where did you hear about us?';

Third: Add the code to retrieve the data

I am going to use four codes. Choose what best suites your need.

A: Get Custom Field value for 1 client. You have client ID and Custom Field Name

$customFieldValue1 =  Capsule::table('tblcustomfields')
                                ->join('tblcustomfieldsvalues','tblcustomfieldsvalues.fieldid','=','tblcustomfields.id')
                                ->where('tblcustomfieldsvalues.relid','=',$clientID)
                                ->where('tblcustomfields.fieldname','=',$customFieldName)
                                ->value('tblcustomfieldsvalues.value');

                                //Use the retrieved vaue e.g echo
                                echo $customFieldValue1."<br/>";

B: Get Custom Field value for 1 client. You have client ID and Custom Field ID

$customFieldValue2 = Capsule::table('tblcustomfieldsvalues')
                                ->where('relid',$clientID)
                                ->where('fieldid',$customFieldID)
                                ->value('value');

                                //Use the retrieved vaue e.g echo
                                echo $customFieldValue2."<br/>";

C: Get Custom Field value for ALL clients. You have client ID and Custom Field Name

$customFieldValues1 =  Capsule::table('tblcustomfields')
                                ->join('tblcustomfieldsvalues','tblcustomfieldsvalues.fieldid','=','tblcustomfields.id')
                                ->select('tblcustomfieldsvalues.value as value','tblcustomfieldsvalues.relid as client')
                                ->where('tblcustomfields.fieldname','=',$customFieldName)
                                ->get();

                                //use retrieved values
                                foreach($customFieldValues1 as $customField){
                                    $clientId = $customField->client;
                                    $customFieldValue = $customField->value;

                                    echo $clientId." : ".$customFieldValue."<br/>";
                                }

C: Get Custom Field values for ALL clients. You have client ID and Custom Field ID

$customFieldValues2 =  Capsule::table('tblcustomfieldsvalues')
                                ->select('relid as client','value')
                                ->where('fieldid',$customFieldID)
                                ->get();

                                //use retrieved values
                                foreach($customFieldValues2 as $customField){
                                    $clientId = $customField->client;
                                    $customFieldValue = $customField->value;

                                    echo $clientId." : ".$customFieldValue."<br/>";
                                }

Please let me know if this answers your question.

For more info on how to write queries with laravel which WHMCS currently uses for interacting with databases, you may visit https://laravel.com/docs/5.2/queries