News:

Support the VirtueMart project and become a member

Main Menu

VM2 AJAX Update Price on Child Product Selection

Started by q-styler, May 19, 2012, 20:15:01 PM

Previous topic - Next topic

q-styler

Hi!
I'm building a webstore with VirtueMart 2.0.6 on Joomla 2.5.4.

So I'm having a fan that has several models with different prices and SKUs of course.

I created a product and some child products. Each of them has its SKU and price. (pic in russian)


Then I created a "Generic Child Variant" custom field with these options:
Published: Yes
Cart Attribute: Yes
Only Administrator: No
List: No
Hidden: No
And named it "Variants"

Then I went to my parent product into Custom Fields tab. Added that custom field choosed SKU in value dropdown list and hit "save".

Now I'm having a dropdown list with SKUs in it on a product page. But when I change its value it doesn't ajax update the price, just redirects browser to the child product url.

I made some investigation on this and found out that and found out that onchange event of the dropdown list generated triest to ajax-get details with these function:
VirtueMartControllerProductdetails::recalculate()
Passing it these data:


And this function just calculates the price of the parent product. Not the child one.
It never triest to access that $field[0]['custom_value'] to get the url of the child product. Neither it deals with quantity (which is another issue).

Please help me!

q-styler

Okay. Here's the solution that worked for me.
I'm not sure if it's the proper one, but since it works it's fine for me. Doublecheck all your functions after applying it.

Since $field[0]['custom_value'] didn't make any sense I went to the html code generation of that dropdown list.
It is located in this file: /administrator/components/com_virtuemart/models/customfields.php on line 765. In function VirtueMartModelCustomfields::displayType()

This file is very unclean and has a lot of comments. So I can presume that it's still under heavy development.

So find this piece of code:
foreach($uncatChildren as $k => $child){
$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']);
}

return JHTML::_('select.genericlist', $options,'field['.$row.'][custom_value]','onchange="window.top.location.href=this.options[this.selectedIndex].value" size="1" class="inputbox"', "value","text",
JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id='.$virtuemart_category_id.'&virtuemart_product_id='.$selected));

break;


This will generate you a dropdown list with "field[0][custom_value]" name and path to the child product as the value of each option.

And change it with these lines:
foreach($uncatChildren as $k => $child){
$options[] = array( 'value' => $child['virtuemart_product_id'] ,'text' =>$child[$value]);
}
// window.top.location.href=this.options[this.selectedIndex].value
return JHTML::_('select.genericlist', $options,'virtuemart_product_id[]','onchange="" size="1" class="inputbox"', "value","text",
JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id='.$virtuemart_category_id.'&virtuemart_product_id='.$selected));

break;


Please note that I removed the onchange javascript code and hid it in the comment since it still tries to redirect me.

I also changed 'text' =>$child['product_name'] to 'text' =>$child[$value] because I want this dropdown list to show not only product_name but the thing I selected in custom fields tab.

I'm really gratefull for anyone who will tell me if this was really bug, or I messed up with the code.

Thank you!