如果父母和孩子已知,获得可配置的选项和价值
-
16-10-2019 - |
题
如果我有可配置的产品ID和关联的子产品ID,如何确定该组合使用的特定属性ID和选项?
解决方案
让我们假设可配置的产品ID是 $mainProductId
儿童产品ID是 $childId
.
您可以获取这样的可配置属性:
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($mainProductId);
$attributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
现在您可以得到这样的子产品价值:
$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 />";
}
}
}