I finally found the way!!
The original code is doing below query:
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
So on the query part we dont need to do anything
but in retrieving part, we can have a change
I have changed:
->where('t2.id', $fieldId)->where('t2.type', 'product')->where('t1.relid', $service['id'])
to:
->where('t2.fieldname', $fieldId)->where('t2.type', 'product')->where('t1.relid', $service['id'])
and
from:
$fieldId = 4; //Custom field id
to:
$fieldId = 'Field Name'; //Custom field name
Now, in the product list of clientarea, we have the custom field value on all products, no matter of field ID, we only need to have a custom field with same name on all products.
so the new code for hook file will be:
<?php
use WHMCS\Database\Capsule as DB;
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
add_hook('ClientAreaPageProductsServices', 1, function($vars) {
$fieldId = 'Field Name'; //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];
}
});
the rest is same as "izghitu" initial codes: