Obtenez option configurable et la valeur si le parent et l'enfant connu
-
16-10-2019 - |
Question
Si j'ai un identifiant de produit configurable et un identifiant de produit associé enfant, comment puis-je déterminer l'attribut id spécifique et les options utilisées pour cette combinaison?
La solution
Supposons que l'ID de produit configurable est $mainProductId
et l'identifiant du produit de l'enfant est $childId
.
Vous pouvez obtenir les attributs configurables comme ceci:
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($mainProductId);
$attributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
Maintenant, vous pouvez obtenir les valeurs des produits de l'enfant comme celui-ci:
$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 />";
}
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange