Jump to content

How to include a custom field in the services list on WHMCS?


Xfireteam

Recommended Posts

1- Create file:

includes/hooks/service_customfields.php 

and add the following code to it:

<?php
use WHMCS\Database\Capsule as DB;
if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

add_hook('ClientAreaPageProductsServices', 1, function($vars) {

    $fieldId = 4; //Custom field id
    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.id', $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];
    }
});

2- In

clientareaproducts.tpl 

use

{$customFields[$service.id]}

to output custom field value.

Note: code is querying custom field value using this SQL:

SELECT *
FROM tblcustomfieldsvalues AS t1
LEFT JOIN tblcustomfields AS t2 ON t1.fieldid = t2.id
WHERE t2.id = 2 #Field id
AND t2.type = 'product' 
AND t1.relid = 9 #service id

 

i trying add it right after

 {$service.product} 

but not work.

Does anyone have any ideas?

I want to show a custom field on each product, or a product ID, to differentiate who it belongs to.

id.png

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.

×
×
  • 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