Here I will show you how we can get the final price of an associated product which is associated with a configurable product.
<?php
$product = Mage::getModel('catalog/product')->load(357); //configurable product
$optProduct = Mage::getModel('catalog/product')->load(282); //associated simple product
$confAttributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
$pdtOptValues = array();
foreach ($confAttributes as $attribute) {
$attrCode = $attribute['attribute_code'];
$attrId = $attribute['attribute_id'];
$optionValue = $optProduct->getData($attrCode);
$pdtOptValues[$attrId] = $optionValue;
}
$product->addCustomOption('attributes', serialize($pdtOptValues));
echo $product->getFinalPrice();
Here above code is very simple to understand. Basically we have instances of both configurable product and associated product. Now we need to add Attribute Custom Options in configurable product instance in order to correctly retrieve the final price of associate product.
For that, we are looping through configurable attributes and prepare attribute options corresponding to the associated product (see the foreach
loop section) and then add that information to configurable product ($product->addCustomOption()
).
That’s it.