Jump to content

Show customfield value in new ticket related services items


AmitTQ

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

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

 

Link to comment
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. 

Link to comment
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}

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

 

Link to comment
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.

Link to comment
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.

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