Get configurable option and value if parent and child known
-
16-10-2019 - |
Question
If I have a configurable product id and an associated child product id, how can i determine the specific attribute id and options used for that combination?
Solution
Let's assume that the configurable product id is $mainProductId
and the child product id is $childId
.
You can get the configurable attributes like this:
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($mainProductId);
$attributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
Now you can get the child product values like this:
$child = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($childId);
foreach ($attributes as $attribute){
//this will get you the id of the option
echo $attribute['store_label'].':'.$child->getData($attribute['attribute_code'])."<br />";
//if you want the label of the option use the foreach below.
foreach ($attribute['values'] as $value){
$childValue = $child->getData($attribute['attribute_code']);
if ($value['value_index'] == $childValue){
echo $attribute['store_label'].':'.$value['store_label']."<br />";
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange