Jump to content
AmitTQ

Show customfield value in new ticket related services items

Recommended Posts

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

Share this post


Link to post
Share on other sites

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']];
    }
});

 

  • Like 1

Share this post


Link to post
Share on other sites

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 ?

 

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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}

Share this post


Link to post
Share on other sites

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']];
    }
});

 

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines