我正在编写一个导入模块,将可配置产品导入magento,效果非常好。我已经调整了导入,以便它可以创建创建可配置产品所需的所有必要属性集,属性和属性选项。到目前为止,一切都有效......一切都很好。
导入创建新属性时,无法创建可配置产品。当我在后端编辑新属性并保存而不进行更改时,会出现一条消息,告诉我更新一些索引。在我更新了产品平面数据索引后,我可以再次运行导入,一切正常。
我尝试过创建新属性的方法:
$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup->addAttribute(
$this->getEntityTypeId(),
$code,
array(
'attribute_code' => $code,
'label' => ucfirst($code),
//'group' => $attributeSet->getId(),
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'input' => 'select',
'unique' => 0,
'required' => 0,
'configurable' => 1,
'filterable' => 1,
'visible_on_front' => 1,
'used_in_product_listing' => 1,
'frontend_label' => array(
$code
)
)
);
另一种方式是:
$attribute = Mage::getModel("catalog/resource_eav_attribute");
$attribute->addData(
array(
'entity_type_id' => $this->getEntityTypeId(),
'attribute_code' => $code,
'label' => ucfirst($code),
//'group' => $attributeSet->getId(),
'is_user_defined' => 1,
'is_global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 0,
'is_configurable' => 1,
'is_filterable' => 1,
'is_visible_on_front' => 1,
'is_used_in_product_listing'=> 1,
'frontend_label' => array(
$code
)
)
);
$attribute->save();
两个代码都很好地创建了属性,但我无法使用它来创建可配置的属性。我试图手动运行索引脚本,但这对我没有帮助。
我做错了什么?以某种方式创造新属性是magento的黑魔法吗? :-D
答案 0 :(得分:0)
我忘了将new属性的后端类型设置为“int”,因此它自动设置为static(这意味着magento在实体表中查找它)。
另一件事是我无法像这样设置属性值:
$model->setData("newattributecode", 123);
Magento没有保存属性。相反,我必须使用未记录的函数Mage_Catalog_Model_Product::addAttributeUpdate()
来保存此模型的属性值:
$model->addAttributeUpdate("newattributecode", 123, 0);