Author Topic: Small customfields hack for Generic Child Variant  (Read 4882 times)

MaartenMJR

  • Beginner
  • *
  • Posts: 4
Small customfields hack for Generic Child Variant
« on: April 18, 2012, 01:02:32 am »
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:
Code: [Select]
$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:

Code: [Select]
$options[] = array( 'value' => $child['virtuemart_product_id'] ,'text' =>$child['product_name']);
And in addition change the selectbox generation into:

Code: [Select]
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:

Code: [Select]
$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.

Panayiotis Fafakos

  • Beginner
  • *
  • Posts: 4
    • Wisdom Software
Re: Small customfields hack for Generic Child Variant
« Reply #1 on: May 26, 2012, 15:46:11 pm »
I think that your code is very interesting.

Can you give as an example of an processChildProduct function?

We are thinking of developing such a function in order to change the image and the other attributes of a product when we navigate through the children of a parent product.

Of course we must do a recalculation like the one that is done in
stockable.php so that we make the correct new prices appear and show the correct modified image.

Thank you in advance,

Panos.
Panayiotis Fafakos
Electronics & Software Engineer

mhuebler

  • Beginner
  • *
  • Posts: 45
    • joomla support
  • VirtueMart Version: 3.0.18
Re: Small customfields hack for Generic Child Variant
« Reply #2 on: November 13, 2013, 14:45:58 pm »
hello,

im sorry to open these old topic again.

the code of MaartenMJR works very well. i have only one problem.

if i try to add the child product to the cart i can see the pop-up and it looks ok. but not the child is insert the cart only the parent product.

anybody can help me with this ?