Jump to content

Display product custom fields on clientareaproducts.tpl page


Chris74

Recommended Posts

Hi we have a problem with customers who have a lot of SSL certificates, or other products where a domain or account username needs to be specified as a custom field.

Using SSL certificates as an example, when the product is created, you can't use the "domain checker" with it to associate a domain with the certificate, because that's not appropriate. So you have to create a custom field for the client to specify which domain name the certificate is for. It's great that this field can be included in invoices - very pleased about that - however, the field doesn't appear anywhere else, other than in the summary at the bottom of clientareaproductdetails.tpl  where all the custom fields are displayed.

This is no good for the customer. We have clients who may have 50 or more SSL certs, or other products that require a custom field to identify them. When looking down their list of products via the clientareaproducts.tpl template, all they see is a long list of the same products with no way to identify what they are for. So if the client wants to manage a particular product, he has to click all of them individually until he finds the one he's looking for. This is a serious issue that we need to resolve.

Is there any way that a hook could be created which could allow the use of an an IF statement in the template  to display a specific custom field, depending on the product? It's easy to identify the fields from the database.

Link to comment
Share on other sites

Hi Chris,

2 hours ago, Chris74 said:

Is there any way that a hook could be created which could allow the use of an an IF statement in the template  to display a specific custom field, depending on the product? It's easy to identify the fields from the database.

one way would be to query the database, create an array of product custom fields for that client and pass it back to the template...

<?php

/**
* Generate Product Custom Fields Array
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function products_list_hook($vars) {

	$client = Menu::context('client');
	
	$productcf = Capsule::table('tblcustomfields')
				->join('tblcustomfieldsvalues','tblcustomfieldsvalues.fieldid','=','tblcustomfields.id')
				->join('tblhosting','tblhosting.id','=','tblcustomfieldsvalues.relid')
				->where('tblhosting.userid',$client->id)
				->where('tblcustomfields.type','product')
				->where('tblcustomfields.fieldtype','text')
				->select('tblhosting.id','tblcustomfields.fieldname','tblcustomfieldsvalues.value')
				->get();
				
	$encodedata = json_encode($productcf);
	$decodedata = json_decode($encodedata, true);
	
	return array("pcf" => $decodedata);
}
add_hook("ClientAreaPageProductsServices", 1, "products_list_hook");
?>

ideally, you'd modify the existing $services array as it would simplify the required Smarty template code - but let's not mess around with that late on a Friday afternoon! :smile2:

so we now have an array of product custom fields available to the clientareaproducts.tpl template, the next step is to change this...

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

to...

                    <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service.domain}" target="_blank">{$service.domain}</a>{else}
						{foreach item=field from=$pcf}
							{if $field.id eq $service.id}
								<br />{$field.value}
							{/if}
						{/foreach}	
					{/if}</td>

cnQFFSg.png

so in the above example, two different products each use a different custom field - IP Address for the first, Domain for the second - to keep things simple, the hook only searches for text custom fields, so ignores dropdowns, checkboxes etc - you could add them in if required, it just complicates the Smarty code a little to have to deal with multiple formats.

additionally, in the above example, each product is only using one text custom field... if they were using multiple text custom fields, they would currently all be shown... now that may be a good or bad thing depending on your intentions for this, but if you knew which fields you wanted to show, you could tweak the if statement in the Smarty code to see if the customfield name matched certain values, or even better, tweak the hook to add tblcustomfields.id to the array (just add it to the select) and check those values.

that's better because the IDs will be unique, whereas you could have multiple custom fields with the same fieldnames, but all with different uses in different products.

9q5eAfo.png

Link to comment
Share on other sites

Wow that's amazing thank you so much!

Each of the products in question has multiple text fields and I'd only want to show one specific field. I'm afraid I don't understand the last part where you explain how to do this. I know the field ID's from the database but I'm not sure how to match those up with the products?

You said... " if you knew which fields you wanted to show, you could tweak the if statement in the Smarty code to see if the customfield name matched certain values ".

Could you possibly provide an example of that?

 

 

Link to comment
Share on other sites

53 minutes ago, Chris74 said:

You said... " if you knew which fields you wanted to show, you could tweak the if statement in the Smarty code to see if the customfield name matched certain values ".
Could you possibly provide an example of that?

so let's say we've modified the hook to give us the custom field ID value and one of the products now has two text custom fields - IP Address and User...

usRQKOG.png

so when we view the clientareaproducts page, we now get...

IHoDlCS.png

we need to remove the custom field with the value of "chris74" - nothing personal, it just has to be that way! :smile2:

so you can see the field IDs that we want to keep are 44 and 45, and the "user" custom field which contains a value of 'chris74' we don't want to keep... so we modify the hook to pass an array of valid field IDs to the template, and tweak the $pcf array to include the fieldid value.

<?php

/**
* Generate Product Custom Fields Array
* @author brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;

function products_list_hook($vars) {

	$client = Menu::context('client');
	$validfields = [44,45];
	
	$productfcf = Capsule::table('tblcustomfields')
				->join('tblcustomfieldsvalues','tblcustomfieldsvalues.fieldid','=','tblcustomfields.id')
				->join('tblhosting','tblhosting.id','=','tblcustomfieldsvalues.relid')
				->where('tblhosting.userid',$client->id)
				->where('tblcustomfields.type','product')
				->where('tblcustomfields.fieldtype','text')
				->select('tblhosting.id','tblcustomfields.fieldname','tblcustomfieldsvalues.fieldid','tblcustomfieldsvalues.value')
				->get();
				
	$encodedata = json_encode($productfcf);
	$decodedata = json_decode($encodedata, true);
	
	return array("pcf" => $decodedata, "validfields" => $validfields);
}
add_hook("ClientAreaPageProductsServices", 1, "products_list_hook");
?>

and then in the template, we can check the current field ID against the list of valid IDs and see if they match... if so, they're shown; if not, they aren't!

                    <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service.domain}" target="_blank">{$service.domain}</a>{else}
						{foreach item=field from=$pcf}
							{if $field.id eq $service.id and in_array($field.fieldid,$validfields)}
								<br />{$field.value}
							{/if}
						{/foreach}
					{/if}</td>

kuO4W6t.png

chris74 has been removed! wave-smiley.gif

a couple of things to note...

1. if you get an error in the hook, and you're using an older version of PHP, you may have to use the longer method to define an array...

$validfields = array("44","45");

2. you can do something similar using fieldnames instead of IDs - but as I said earlier, i'd personally prefer to use IDs...

$validfields = ['Domain','User'];
                    <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service.domain}" target="_blank">{$service.domain}</a>{else}
						{foreach item=field from=$pcf}
							{if $field.id eq $service.id and in_array($field.fieldname,$validfields)}
								<br />{$field.value}
							{/if}
						{/foreach}
					{/if}</td>

XfXiINc.png

chris74 has returned because I chose 'User' instead of "IP Address" in the $validfields array - welcome back Chris!

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • 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