Jump to content

Dropdown custom field key value pair


wtools

Recommended Posts

I have some custom fields(drop down) fro the products and those custom field details are stored in the table tblcustomfields

I can see that there is a field fieldoptions, which stores drop down values as a comma separated list like 1,2,3 etc.

in this case we will see 1,2 and 3 in the drop down like

<option value="1">1</option><option value="2">2</option><option value="3">3</option>

Is it possible to populate a custom field dropdown as key value pair?

So that the customer will see keys and while submiting the form , diffrent values will be posted.

So the final html should be

<option value="1">user1</option><option value="2">user2</option><option value="3">user</option>.

 

Is it possible?

 

 

Link to comment
Share on other sites

5 hours ago, wtools said:

Is it possible?

as the HTML is generated internally by WHMCS, you have the usual two options - manipulate it in the template (using Smarty), or via an action hook (using PHP)...

so at the most basic, the Smarty change would be..

{$customfield.input|replace:'>1':'>user1'|replace:'>2':'>user2'|replace:'>3':'>user3'}

now you may also need to wrap that in an {if} statement to ensure only that specific customfield is modified in this way.

if you wanted to do it as a hook for a specific customfield (remembering to change the ID value to suit your needs!), it would be along the lines of...

<?php

/**
* Modify Custom Client Fields
* @author brian!
*/

function modify_custom_client_fields_hook($vars) {

	$oldkey = array(">1", ">2", ">3");
	$newkey = array(">user1", ">user2", ">user3");
	
	foreach ($vars['customfields'] as $ccf => $value) {
		if ($vars['customfields'][$ccf]['id'] == '49') {
			$vars['customfields'][$ccf]['input'] = str_replace($oldkey,$newkey,$value['input']);
		}
	}
        
	return array("customfields" => $vars['customfields']);
}
add_hook("ClientAreaPageProfile",1,"modify_custom_client_fields_hook");
add_hook("ClientAreaPageCart",1,"modify_custom_client_fields_hook");

the above will generate the following output...

<option value="1">user1</option><option value="2">user2</option><option value="3">user3</option>

in either case (Smarty or hook), remember to be careful when choosing what to replace and ensure that your search term is unique.

if this custom client field isn't shown during ordering, then you can remove the final ClientAreaPageCart line from the hook... though if you are not using a specific ID in your hook, then take care when using ClientAreaPageCart as it will run on every cart page and so might also modify product custom fields too.

i'm being lazy in not specifying which cart pages it should run on, but there's nothing to stop you adding an if statement as below... older versions of WHMCS could use an alternative ClientAreaPage hook if they prefer...

<?php

/**
* Modify Custom Client Fields on Profile Page
* @author brian!
*/

function modify_custom_client_fields_hook($vars) {
	
	if ($vars['templatefile'] == "clientareadetails" || $vars['templatefile'] == "viewcart") {

		$oldkey = array(">1", ">2", ">3");
		$newkey = array(">user1", ">user2", ">user3");
	
		foreach ($vars['customfields'] as $ccf => $value) {
			if ($vars['customfields'][$ccf]['id'] == '49') {
				$vars['customfields'][$ccf]['input'] = str_replace($oldkey,$newkey,$value['input']);
			}
		}

		return array("customfields" => $vars['customfields']);
	}
}
add_hook("ClientAreaPage",1,"modify_custom_client_fields_hook");

 

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