Remitur Posted January 11, 2019 Share Posted January 11, 2019 (edited) 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... 😞  Edited January 11, 2019 by Remitur 0 Quote Link to comment Share on other sites More sharing options...
mbit Posted January 12, 2019 Share Posted January 12, 2019 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 } Â 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 5, 2019 Share Posted March 5, 2019 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! 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 5, 2019 Share Posted March 5, 2019 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. 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 5, 2019 Share Posted March 5, 2019 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 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 5, 2019 Share Posted March 5, 2019 I understand your goals, but not your current issue. Did you try the above solution and complete it to your needs? If so, what was the actual results? 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 6, 2019 Share Posted March 6, 2019 Tried, but somehow I cant get it to work! 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 6, 2019 Share Posted March 6, 2019 $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 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 6, 2019 Share Posted March 6, 2019 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. 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 6, 2019 Share Posted March 6, 2019 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"); } }  0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 7, 2019 Share Posted March 7, 2019 @steven99 Then I get a beautiful message like ParseError: syntax error, unexpected 'foreach' (T_FOREACH) Â Â 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 7, 2019 Share Posted March 7, 2019 Okay, then it is something before the foreach that isn't being terminated with a semi-colon (";") . Would need to see all the code to know for sure. 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 7, 2019 Share Posted March 7, 2019 We get the value now! - but another problem occured described in new topic. The problem was that there had came som strange charchers in the php file that was not visible in Notepad ++ but in Netbeans, Thanks to @steven99 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 9, 2019 Share Posted March 9, 2019 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"); } } Â 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 9, 2019 Share Posted March 9, 2019 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) 🙂 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 10, 2019 Share Posted March 10, 2019 Again, you need to change $xml .= $field->customField->value; // Add ID to XML to $xml .= $field->value; // Add ID to XML 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 10, 2019 Share Posted March 10, 2019 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! 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 10, 2019 Share Posted March 10, 2019 $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?!   0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 11, 2019 Share Posted March 11, 2019 Does anybody know at all a good way to retrive value from Client Custom Field that actually works? Please advice! 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted March 11, 2019 Share Posted March 11, 2019 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 ?? 0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 11, 2019 Share Posted March 11, 2019 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 .="  0 Quote Link to comment Share on other sites More sharing options...
ptomter Posted March 11, 2019 Share Posted March 11, 2019 What am I missing now in my last post to get this to work? 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 12, 2019 Share Posted March 12, 2019 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; });  1 Quote Link to comment Share on other sites More sharing options...
BakH Posted April 29, 2019 Share Posted April 29, 2019 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. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.