AmitTQ Posted February 16, 2019 Share Posted February 16, 2019 Hi friends, I am looking for a way to bring a custom product field to new ticket service selector items.I did it for products list page using below code as hook.php: <?php use WHMCS\Database\Capsule as DB; if (!defined("WHMCS")) { die("This file cannot be accessed directly"); } add_hook('ClientAreaPageProductsServices', 1, function($vars) { $fieldId = 'Product Serial Number'; //Custom field Name if (isset($vars['services'])) { $csVals = []; foreach ($vars['services'] as $service) { $fieldVal = ''; $data = DB::table('tblcustomfieldsvalues AS t1') ->leftJoin('tblcustomfields AS t2', 't1.fieldid', '=', 't2.id') ->select('t1.value') ->where('t2.fieldname', $fieldId)->where('t2.type', 'product')->where('t1.relid', $service['id']) ->first(); if (!is_null($data)) { $fieldVal = $data->value; } $csVals[$service['id']] = $fieldVal; } return ['customFields' => $csVals]; } }); then used it in templates/six/clientareaproducts.tpl as: {$customFields[$service.id]} But I am not able to do the same with the supportticketsubmit-steptwo.tpl to have same output on new ticket product list. Any idea ? BR 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted February 18, 2019 Share Posted February 18, 2019 What hook did you use for the support ticket submit page? Should be using ClientAreaPageSubmitTicket . Try this hook <?php add_hook('ClientAreaPageSubmitTicket', 1, function($vars) { if (isset($vars['client'])) { foreach($vars['relatedservices'] as $key => $S) { $service = WHMCS\Service::find($S['id']); if ($service) { foreach ($service->customFieldValues as $field) { if ($field->customField->fieldName == "t2") { $vars['relatedservices'][$key]['ServiceID'] = $field->value; } } } } return ['relatedservices'=>$vars['relatedservices']]; } }); 1 Quote Link to comment Share on other sites More sharing options...
AmitTQ Posted February 22, 2019 Author Share Posted February 22, 2019 I used same code and only changed the hook to: add_hook('ClientAreaPageSubmitTicket', 1, function($vars) { so if I used your shared hook, what should i use in .tpl file to echo the custom field value to the page ? 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 1, 2019 Share Posted March 1, 2019 In the relatedservices foreach loop in supportticketsubmit-steptwo.tpl, you would change the option from: {$relatedservice.name} ({$relatedservice.status}) to {$relatedservice.name} ({$relatedservice.status}) - ({$relatedservice.ServiceID}) where $relatedservice.ServiceID is the value returned in the hook's replacement. 0 Quote Link to comment Share on other sites More sharing options...
AmitTQ Posted March 1, 2019 Author Share Posted March 1, 2019 I created a hook-file.php and pasted your code and faced with "Oops!" message with below details: Error: Call to undefined method WHMCS\Service::find() in .../includes/hooks/service_customfields_clientareapagesubmitticket.php:14#0 .../includes/hookfunctions.php(0): WHMCS\Utility\SafeInclude::{closure}(Array)#1 .../includes/clientareafunctions.php(0): run_hook('ClientAreaPageS...', Array)#2 .../submitticket.php(0): outputClientArea('supportticketsu...', false, Array)#3 {main} 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 1, 2019 Share Posted March 1, 2019 Didn't give complete internal class to use (missing the second service) <?php add_hook('ClientAreaPageSubmitTicket', 1, function($vars) { if (isset($vars['client'])) { foreach($vars['relatedservices'] as $key => $S) { $service = WHMCS\Service\Service::find($S['id']); if ($service) { foreach ($service->customFieldValues as $field) { if ($field->customField->fieldName == "t2") { $vars['relatedservices'][$key]['ServiceID'] = $field->value; } } } } return ['relatedservices'=>$vars['relatedservices']]; } }); 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 1, 2019 Share Posted March 1, 2019 Just noticed that the IDs in each service is prefixed with a "S" or "D" depending on which one it is referring to. So added a ltrim to get the real ID and tested this on dev install. <?php add_hook('ClientAreaPageSubmitTicket', 1, function($vars) { if (isset($vars['client'])) { foreach($vars['relatedservices'] as $key => $S) { $ID = ltrim($S['id'], "SD"); // WHMCS puts "D" for domain and "S" for service in front of the id for reasons if (is_numeric($ID)) { $service = WHMCS\Service\Service::find($ID); if ($service) { foreach ($service->customFieldValues as $field) { if ($field->customField->fieldName == "AccountID") { $vars['relatedservices'][$key]['ServiceID'] = $field->value; } } } } } return ['relatedservices'=>$vars['relatedservices']]; } }); The same tpl changes apply. 1 Quote Link to comment Share on other sites More sharing options...
AmitTQ Posted March 2, 2019 Author Share Posted March 2, 2019 20 hours ago, steven99 said: Just noticed that the IDs in each service is prefixed with a "S" or "D" depending on which one it is referring to. So added a ltrim to get the real ID and tested this on dev install. <?php add_hook('ClientAreaPageSubmitTicket', 1, function($vars) { if (isset($vars['client'])) { foreach($vars['relatedservices'] as $key => $S) { $ID = ltrim($S['id'], "SD"); // WHMCS puts "D" for domain and "S" for service in front of the id for reasons if (is_numeric($ID)) { $service = WHMCS\Service\Service::find($ID); if ($service) { foreach ($service->customFieldValues as $field) { if ($field->customField->fieldName == "AccountID") { $vars['relatedservices'][$key]['ServiceID'] = $field->value; } } } } } return ['relatedservices'=>$vars['relatedservices']]; } }); The same tpl changes apply. Thank you Steve! this hook works perfectly. 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.