Jump to content

custom client fields values retrieving


Remitur

Recommended Posts

Perhaps something like this...

<?php
use WHMCS\User\Client;

// get client's custom fields
$clientFields = Client::find($userID)->customFieldValues;

// run through customfields
foreach($clientFields AS $field){
  //...do something
}

 

Link to comment
Share on other sites

  • 1 month later...
On 1/11/2019 at 3:44 PM, Remitur said:

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

Did you find a solution to your problem ? Im facing a similar problem!

Link to comment
Share on other sites

27 minutes ago, steven99 said:

Have you tried the solution mentioned by @mbit ?  That will give you the values you want.  Check WHMCS/User/Client and CustomFieldValue for more info on those objects. 

 

Ok then we continue here.

I now the fieldname as I created it in custom client fields.

In my custom module I want to get the value from customclient field (from the client)

So Example

Customfield = Fax

I want to have the Faxnumber value passed from that client to use to set up a product

Link to comment
Share on other sites

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

	 
	 if (!$fieldname)
        {
        	return array("error"=>"Some Error Message");
        }
        else
			
        { $xml .="
			<no-ext-contact:identity type=\"Number\">
			$fieldname1
			</no-ext-contact:identity>";
        } $xml .="
	 }
	 
	

1) Check if there is a value in fieldname1

2) fieldname1 value also used in the XML

Please advice
Link to comment
Share on other sites

On 1/11/2019 at 3:44 PM, Remitur said:

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

DId you find any good solution to your problem. ? Please let me know and share your thoughts and what solution you came up with.

Link to comment
Share on other sites

You need to get the field name by $field->customField->fieldName  where you are trying to get the fieldname from a undefined variable .

For example:

foreach($clientFields AS $field)
{
    if (isset($field->customField->fieldName))
    {
        $xml .= "
			<no-ext-contact:identity type='Number'>" .
            $field->customField->fieldName . "
			</no-ext-contact:identity>";
    }
    else
    {
        return array("error" => "Some Error Message");

    }
}

 

Link to comment
Share on other sites

Im not quite sure if we are getting the value at all ... as now my xml error is
Parse error at line [28], column [1824]: Element 'identity': ' ' is not a valid value of the atomic type. As its empty not getting any value.
Last code we have and tried is as follow.

 

foreach($clientFields AS $field)
{
    
if (isset($field->customField->fieldName) 
	
and $field->customField->fieldName == "CustomFieldName") 

{ 
    $xml .= $field->customField->value; // Add ID to XML 
}	
    
    else
    {
        return array("error" => "Some Error Message");

    }
}

 

Link to comment
Share on other sites

On 1/11/2019 at 3:44 PM, Remitur said:

Hello.


Does exist a way (different from a direct mysql query) to retrieve the values of custom client fields for a certain customer?

I looked around in API and documentation, but was not able to find anything about... 😞

 

Did you find a solution, and if so please share your solution with the community (and me) 🙂

Link to comment
Share on other sites

9 hours ago, steven99 said:

Again, you need to change


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

to


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

Yes its correct, but its still not getting any values at all into the xml, but at least the xml is right just not getting any value!

Link to comment
Share on other sites

$clientFields = Client::find($userID)


Would alone return null as $userID is not defined.

But when looking at there should be an easy way to get value for field when knowing the field name.  As there are allready are methods that obtain all the client custom fields. Property for name of custom field, and property for custom field values.

 

https://docs.whmcs.com/classes/7.6/WHMCS/CustomField.html

 

   
Anyone?!

 

 

Link to comment
Share on other sites

2 hours ago, ptomter said:

Does anybody know at all a good way to retrieve value from Client Custom Field that actually works? Please advice!

if the client was logged in, then their client custom field values would be available from the $clientsdetails array - which in a hook you could access via $vars

other than querying the database (which is what I would do), then using the class docs as above should work without any issue... though obviously you'll have to define the value of $userID - which I assume that you are doing somewhere ??

Link to comment
Share on other sites

30 minutes ago, brian! said:

if the client was logged in, then their client custom field values would be available from the $clientsdetails array - which in a hook you could access via $vars

other than querying the database (which is what I would do), then using the class docs as above should work without any issue... though obviously you'll have to define the value of $userID - which I assume that you are doing somewhere ??

Hi @brian!I think there is a misunderstanding somewhere... as this is a custom module, and I try to get the value when I use the module on the customer in the Admin Section.  So im not logged in as the customer!

As It seems that some misunderstanding has been the issue I also wrote a sql query that works in sql that I had hope to use in the meantime. Yes I know its not Laravel and Capsule yet and full_query may be removed from later version, but for now I need a solution to work so I can get the value from the client customer field. to use in the XML (custom module) from Admin Section on a Client, when setting up a product. As there is no auto setup if customer doesnt pay the invoice right away. Then this must be done manually from admin.

$val = 
full_query("
SELECT b.value
FROM tblcustomfieldsvalues b
JOIN tblcustomfields a 
ON a.id = b.fieldid
AND a.fieldname= 'FieldName'
AND b.relid = '1';  // Here could I have $params[customerid] to get the id
"); 
	   
if (isset($val))
	
{$xml .=  $val;} 	
    
else
{
return array("error" => "Field is Empty");
}
	
 $xml .="


 

Link to comment
Share on other sites

Please see hooks I used for testing below.  It outputs all custom fields for the client custom fields and for their service custom fields.   I have tested this with WHMCS 7.6 and outputs as expected.  First toss that the hooks in to a file and toss that in to whmcs_root/includes/hooks to confirm they are getting the custom fields.  If not, what version of WHMCS are you using?  

In your XML items, just use what I have given below in the foreach loops where it mentions values to fill in the needed areas.  If you still have problems with this, all I can really say is to hire a developer via the requests community. 

I did both client custom fields and service custom fields just in case you or someone else needs either one.  I really doesn't matter where you are calling from as long as you give it a userid .

<?php
/**
 * @author steven99
 */

use WHMCS\User\Client;

add_hook('AdminAreaClientSummaryPage', 1, function($vars) {
    // get client's custom fields
    $HTML = "Client Custom Fields<br>";
    $Client = Client::find($vars['userid']);
    $clientFields = $Client->customFieldValues;

    // run through customfields
    foreach($clientFields AS $field){
        $HTML .= "Field ".$field->customField->fieldName.": ".$field->value."<br>";
    }

    $HTML .= "Service custom fields";
    foreach($Client->services as $service)
    {
        $ServiceFields = $service->customFieldValues;
        foreach($ServiceFields as $field)
        {
            $HTML .= "Service field ".$field->customField->fieldName . ": ".$field->value."<br>";

        }
    }
    return $HTML;
});

//GET VIA API
add_hook('AdminAreaClientSummaryPage', 1, function($vars) {

    $results = localAPI('GetClientsDetails', array(
        'clientid' => $vars['userid'])
    );
    $HTML = "Client Custom Fields from API<br>";

    if ($results['result'] == "success")
    {
        foreach($results as $fieldkey => $fieldName)
        {
            //WHMCS likes to have custom fields returned by API in both clientdetails array and the main array as a
            // variable called "customfieldsX" where "X" is a number.  We use this number to find our index within the
            // customfields array.
            if (preg_match("/customfields(\d+)/", $fieldkey, $Match))
            {
                $FieldID = $Match[1] - 1; // arrays start with 0, but we get a 1 as starter so minus 1 to get the correct index
                if (isset($results['customfields'][ $FieldID ]))
                {
                    $HTML .= "Field $fieldName: ". $results['customfields'][ $FieldID ]['value']."<br>";
                }
            }
        }
    }


    $HTML .= "Service Custom Fields from API<br>";
    $results = localAPI('GetClientsProducts', $postData = array(
        'clientid' => $vars['userid'],));
    if ($results['result'] == 'success')
    {
        foreach($results['products']['product'] as $service)
        {
            foreach($service['customfields']['customfield'] as $field)
            {
                $HTML .= "Field".$field['name'].": ".$field['value']."<br>";
            }
        }
    }

    return $HTML;

});

 

Link to comment
Share on other sites

  • 1 month later...
On 3/12/2019 at 5:43 AM, steven99 said:

Please see hooks I used for testing below.  It outputs all custom fields for the client custom fields and for their service custom fields.   I have tested this with WHMCS 7.6 and outputs as expected.  First toss that the hooks in to a file and toss that in to whmcs_root/includes/hooks to confirm they are getting the custom fields.  If not, what version of WHMCS are you using?  

In your XML items, just use what I have given below in the foreach loops where it mentions values to fill in the needed areas.  If you still have problems with this, all I can really say is to hire a developer via the requests community. 

I did both client custom fields and service custom fields just in case you or someone else needs either one.  I really doesn't matter where you are calling from as long as you give it a userid .


<?php
/**
 * @author steven99
 */

use WHMCS\User\Client;

add_hook('AdminAreaClientSummaryPage', 1, function($vars) {
    // get client's custom fields
    $HTML = "Client Custom Fields<br>";
    $Client = Client::find($vars['userid']);
    $clientFields = $Client->customFieldValues;

    // run through customfields
    foreach($clientFields AS $field){
        $HTML .= "Field ".$field->customField->fieldName.": ".$field->value."<br>";
    }

    $HTML .= "Service custom fields";
    foreach($Client->services as $service)
    {
        $ServiceFields = $service->customFieldValues;
        foreach($ServiceFields as $field)
        {
            $HTML .= "Service field ".$field->customField->fieldName . ": ".$field->value."<br>";

        }
    }
    return $HTML;
});

//GET VIA API
add_hook('AdminAreaClientSummaryPage', 1, function($vars) {

    $results = localAPI('GetClientsDetails', array(
        'clientid' => $vars['userid'])
    );
    $HTML = "Client Custom Fields from API<br>";

    if ($results['result'] == "success")
    {
        foreach($results as $fieldkey => $fieldName)
        {
            //WHMCS likes to have custom fields returned by API in both clientdetails array and the main array as a
            // variable called "customfieldsX" where "X" is a number.  We use this number to find our index within the
            // customfields array.
            if (preg_match("/customfields(\d+)/", $fieldkey, $Match))
            {
                $FieldID = $Match[1] - 1; // arrays start with 0, but we get a 1 as starter so minus 1 to get the correct index
                if (isset($results['customfields'][ $FieldID ]))
                {
                    $HTML .= "Field $fieldName: ". $results['customfields'][ $FieldID ]['value']."<br>";
                }
            }
        }
    }


    $HTML .= "Service Custom Fields from API<br>";
    $results = localAPI('GetClientsProducts', $postData = array(
        'clientid' => $vars['userid'],));
    if ($results['result'] == 'success')
    {
        foreach($results['products']['product'] as $service)
        {
            foreach($service['customfields']['customfield'] as $field)
            {
                $HTML .= "Field".$field['name'].": ".$field['value']."<br>";
            }
        }
    }

    return $HTML;

});

 

I have really loved this. Bravo @steven99.

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