我创建了新的可配置产品,并用 PHP 附加了他们的简单产品。
现在,当我编辑任何可配置的产品时,我会看到这个屏幕:
那么在没有任何 Magento 文档的情况下,我应该在 PHP 中调用什么来以编程方式执行与上面屏幕相同的功能?
我已经看到一些示例中使用了 $configurable_product->setConfigurableProductsData()
,但我认为这不是我需要的。
最佳答案
您是对的,您正在创建可配置产品和子产品之间的关联/链接,但实际情况是,当您创建可配置产品时,您没有设置基本上设置 super 属性的 setConfigurableAttributesData该可配置产品的信息。
foreach($configAttrCodes as $attrCode){
$super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode->code);
$configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
$newAttributes[] = array(
'id' => $configurableAtt->getId(),
'label' => $configurableAtt->getLabel(),
'position' => $super_attribute->getPosition(),
'values' => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(),
'attribute_id' => $super_attribute->getId(),
'attribute_code' => $super_attribute->getAttributeCode(),
'frontend_label' => $super_attribute->getFrontend()->getLabel(),
);
}
$existingAtt = $product->getTypeInstance()->getConfigurableAttributes();
if(empty($existingAtt) && !empty($newAttributes)){
$configProduct->setCanSaveConfigurableAttributes(true);
$configProduct->setConfigurableAttributesData($newAttributes);
$configProduct->save();
}
这是一个小片段应该可以帮助您,如果您有任何问题或需要进一步的帮助,请告诉我。
关于php - Magento 'Select Configurable Attributes' 与 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8538790/