See:
$product->getTypeInstance()->getProductByAttributes($attributesInfo, $product);
from Mage_Catalog_Model_Product_Type_Configurable
.
Edit:
This is a question like the catchy ones from job interviews.
Do you need this on configurable product page on frontend ?
In app/code/base/default/template/catalog/product/type/options/configurable.phtml
there is:
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
You need to extend this object. Either add your function in the class or extend an existing function that you'll need. http://prototypejs.org/learn/class-inheritance
In the JSON config passed to spConfig by default there is the options
key and a deeper products
key. You can use this config in your JS function. On any configurable product page copy paste the JSON object and format it with a tool for ease of understanding.
The JSON is built in Mage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig()
.
In the JS class there is:
this.settings.each(function(element){
Event.observe(element, 'change', this.configure.bind(this))
}.bind(this));
In the configureElement
function there is the call:
this.reloadPrice();
In function reloadPrice
an iteration on the dropdowns is made.
Your code should mimic this iteration to check if all available dropdowns were selected.
On success your custom code comes in.
Is it an Ajax call to fetch dynamic content ? I would not recommend it. Instead try to have the dynamic content/data of associated products available in the rendered page in a JSON object handled by you custom JS class.
I got the solution to the above requirment..
$product= Mage::getModel('catalog/product')->load($configId);
#Check if the product has children
if ($product->type_id == 'configurable') {
$options = array();
// Get any super_attribute settings we need
$productAttributesOptions = $product->getTypeInstance(true)->getConfigurableOptions($product);
foreach ($productAttributesOptions as $productAttributeOption) {
$options[$product->getId()] = array();
foreach ($productAttributeOption as $optionValues) {
$val = $this->trimValue($optionValues['option_title']);
$options[$product->getId()][] = array (
$optionValues['sku'] => $optionValues['attribute_code']."=".$val
) ;
}
}
Best Answer
I found an answer for this question if we send it params and parent product it will return the related associated product.
Hope it will help someone else..