0

I'm using WHMCS 7.x and Smarty PHP is already enabled.

I want to show in the client area products (clientareaproducts.tpl) below the name of the product, a custom field which already exists and it's filled with an IP adress ( the smarty PHP code for that custom field is {$service_custom_fields.4} ) instead of $service.domain.

Below is the clientareaproducts.tpl file:

{include file="$template/includes/tablelist.tpl" tableName="ServicesList" filterColumn="3"}
<script type="text/javascript">
    {if $orderby == 'product'}
        table.order([0, '{$sort}'], [3, 'asc']);
    {elseif $orderby == 'amount' || $orderby == 'billingcycle'}
        table.order(1, '{$sort}');
    {elseif $orderby == 'nextduedate'}
        table.order(2, '{$sort}');
    {elseif $orderby == 'domainstatus'}
        table.order(3, '{$sort}');
    {/if}
    table.draw();
    jQuery('#tableLoading').addClass('hidden');
});
</script>
    <div class="table-container clearfix">
<table id="tableServicesList" class="table table-list hidden">
    <thead>
        <tr>
            <th>{$LANG.orderproduct}</th>
            <th>{$LANG.clientareaaddonpricing}</th>
            <th>{$LANG.clientareahostingnextduedate}</th>
            <th>{$LANG.clientareastatus}</th>
            <th class="responsive-edit-button" style="display: none;"></th>
        </tr>
    </thead>
    <tbody>
        {foreach key=num item=service from=$services}
            <tr onclick="clickableSafeRedirect(event, 'clientarea.php?action=productdetails&amp;id={$service.id}', false)">
                <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service.domain}" target="_blank">{$service.domain}</a>{/if}</td>
                <td class="text-center" data-order="{$service.amountnum}">{$service.amount}<br />{$service.billingcycle}</td>
                <td class="text-center"><span class="hidden">{$service.normalisedNextDueDate}</span>{$service.nextduedate}</td>
                <td class="text-center"><span class="label status status-{$service.status|strtolower}">{$service.statustext}</span></td>
                <td class="responsive-edit-button" style="display: none;">
                    <a href="clientarea.php?action=productdetails&amp;id={$service.id}" class="btn btn-block btn-info">
                        {$LANG.manageproduct}
                    </a>
                </td>
            </tr>
        {/foreach}
    </tbody>
</table>
<div class="text-center" id="tableLoading">
    <p><i class="fa fa-spinner fa-spin"></i> {$LANG.loading}</p>
</div>

I've tried replacing the following line:

 <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service.domain}" target="_blank">{$service.domain}</a>{/if}</td>

with

 <td><strong>{$service.product}</strong>{if $service_custom_fields.4}<br /><a href="http://{$service_custom_fields.4}" target="_blank">{$service_custom_fields.4}</a>{/if}</td>

but unfourtunately, it doesn't work.

The smarty php code works on the email templates, but not here.

I've also tried with the following line by adding a domain to the product, but it doesn't show the custom field either.

 <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service_custom_fields.4}" target="_blank">{$service_custom_fields.4}</a>{/if}</td>

I did that because i thought domainstatus

{elseif $orderby == 'domainstatus'}

would be setted to false if the domain was empty and by adding a domain, it would then be setted to true, and it would show the custom field, but it doesn't work.

I'd really appreciate if someone could please help me.

Regards.

1 Answers1

0

WHMCS doesn't populate the custom fields for the services in this page. In general you can check what is available for you by using smarty function debug_print_var inside the tpl file:

{$service|@debug_print_var}

Now, you need to take care of populating the custom field you want. This can be done using hooks.

The hook ClientAreaPageProductsServices will be called when clientareaproducts page is visited, and it presents all template variables as function parameter $vars.

We're going to loop through $vars['services'] and query custom field values from Db and pass them as separate variable that you can use in tpl file:

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
wesamly
  • 1,484
  • 1
  • 16
  • 23
  • Hello, first of all, thanks for your reply. I've added the code to the hooks section and replaced with `{$customFields[$service.id]}` inside the .tpl file but it doesn't work, it's not showing the IP adress below the product name. Maybe it has something to do with this line `{elseif $orderby == 'domainstatus'}` being disabled if there's no domain configured? –  Nov 05 '17 at 19:23
  • Try not to add it inside if, add it right after {$service.product} for now, just to make sure it is working. – wesamly Nov 06 '17 at 06:25