Improving the extra field handling in X-Cart 4
One of the first changes I perform to the X-Cart 4 core code as a theme developer is to improve the way Extra Fields are held on the product page.
Extra Fields are a great way to add custom fields to a product that can be used on the product page. So for example a store owner would ask me to add an option in the backend at product level which will allow them to show a ‘Made in the UK’ flag at product level. The store owner would like to control each product individually and also be able to use the mass modify option too.
The main problem for the developer is that the extra fields array is held in an array that is not indexed efficiently. So you would need to loop each array element of extra fields and then test if it is populated and active.
The problem with this is that it is a processing overhead (not massive) but also makes your code untidy in the smart template.
The extra field array would look like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
{$extra_fields} Array (9) 0 => Array (9) fieldid => "2" provider => "1" field => "Cost Price" value => "" active => "" orderby => "1" service_name => "cost_price" field_value => "22.00" is_value => "Y" 1 => Array (9) fieldid => "4" provider => "1" field => "Lowest Sale Price" value => "" active => "" orderby => "2" service_name => "lowest_sale_price" field_value => null is_value => "" 2 => Array (9) fieldid => "3" provider => "1" field => "Material" value => "" active => "Y" orderby => "3" service_name => "material" field_value => "Sterling Silver" is_value => "Y" <span style="color: #008000;">3 => Array (9) fieldid => "5" provider => "1" field => "Made in the UK Flag" value => "Y" active => "Y" orderby => "4" service_name => "madeinuk" field_value => null is_value => "" </span> |
If you edit the file modules\Extra_Fields\extra_fields.php
find
1 |
$smarty->assign('extra_fields', $extra_fields); |
1 2 3 4 5 6 7 8 9 |
foreach ($extra_fields as $k => $v) { $extra_fields_idx[$v['service_name']]['value'] = func_xss_free($v['field_value']); if ($v['service_name'] == "gender") $extra_fields_idx[$v['service_name']]['items'] = explode("|",func_xss_free($v['field_value'])); } $smarty->assign('extra_fields_idx', $extra_fields_idx); |
1 2 3 4 5 6 7 8 9 10 |
{$extra_fields_idx} Array (9) cost_price => Array (1) value => "22.00" lowest_sale_price => Array (1) value => null material => Array (1) value => "Sterling Silver" <span style="color: #008000;">madeinuk => Array (1) value => "Y" </span> |
1 |
{if $extra_fields_idx.madeinuk eq 'Y'}<img src="urltotheukflag.jpg" alt="alt tag text" />{/if} |