In Virtuemart 2 (I'm currently using 2.0.2) one can use the Generic Child Variant custom field to present different attributes of a certain parent product (e.g. the sizes S, M, L, XL for a T-Shirt), where the stock quantities of the separate children are managed by Virtuemart. The current implementation redirects automatically to the child product detail page, which is hard-coded into customfields.php. While implementing VM on a webshop, I realised that I didn't like the automatic redirect and I found that the code around line 702 in customfields.php isn't quite generic and could easily be changed.
The current customfields.php line 702 reads:
$options[] = array( 'value' => JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id='.
$virtuemart_category_id.'&virtuemart_product_id='.$child['virtuemart_product_id']) ,'text' =>$child['product_name']);
My proposal would be to change this into:
$options[] = array( 'value' => $child['virtuemart_product_id'] ,'text' =>$child['product_name']);
And in addition change the selectbox generation into:
return JHTML::_('select.genericlist', $options,'field['.$row.'][custom_value]',
'onchange="processChildProduct(this.options[this.selectedIndex])" size="1" class="inputbox"', "value","text",
JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id='.$virtuemart_category_id.'&virtuemart_product_id='.$selected));
As a consequence, the product detail template has to provide an proper processChildProduct function in order to redirect to a child product page, but one is then, as a developer, free to implement another script. In this particular website, I choose to update the virtuemart form with the values (name and id) retrieved from the selectbox. The current, expiremental, code that I added to the productdetails template reads:
$document->addScriptDeclaration("
function changeFormProperties(option)
{
var val=document.product_form.elements[3].value;
document.product_form.elements[3].value = val.substr( 0, val.lastIndexOf(' ')) + ' ' + option.text;
document.product_form.elements['virtuemart_product_id[]'].value = option.value;
}
jQuery(document).ready(function() {
var val='';
for(var i=0; i<document.getElementById('field0custom_value').length; i++) {
val=document.getElementById('field0custom_value').options[i].text;
document.getElementById('field0custom_value').options[i].text=val.substr( val.lastIndexOf(' ') );
}
});
");
Which, while loading the page, extracts the sizes (that form the last word of the product name) and updates the underlying form with the child id and name.
I would appreciate comments on this, and, finally, I would be very happy if a generic solution to my problem could be added to the virtuemart core.