我到处寻找答案,通过Magento混淆,我找不到合适的解决方案。我正在动态地创建magento属性,这很好,但是当谈到
时这是我添加属性的代码
$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->addAttribute('catalog_product', $name, array(
'type' => 'varchar',
'input' => 'select',
'backend' => '',
'frontend' => '',
'label' => $key,
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible' => true,
'visible_on_front' => true,
'visible_in_advanced_search' => true,
'unique' => false,
'apply_to' => '',
'is_configurable' => false,
'option' => array(
'values' => array($specific)
)
));
$installer->endSetup();
$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key))->save();
它添加它很好,它甚至为我添加了选项,但我似乎无法将该选项设置为默认值(稍后再添加)并且我无法使其可搜索。
我真的希望有人可以提供帮助。
由于
更新
好的,我已经设法使用此代码添加默认值(仍然无法搜索等)。
$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->startSetup();
$installer->addAttribute('catalog_product', $name, array(
'type' => 'int',
'input' => 'select',
'backend' => '',
'frontend' => '',
'label' => $key,
'class' => '',
'source' => 'eav/entity_attribute_source_table',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible' => true,
'visible_on_front' => true,
'visible_in_advanced_search' => true,
'unique' => false,
'apply_to' => '',
'is_configurable' => false,
'option' => array(
'value' => array(
$this->getAttributeName($specific) => array($specific)
)
)
));
$installer->endSetup();
$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key));
$attr->setDefaultValue($attr->getSource()->getOptionId($this->getAttributeName($specific)));
$attr->save();
然而,当我使用此代码添加新选项时,$ specific =“Pepsi”;
$model = Mage::getModel('catalog/resource_eav_attribute');
$option = array();
$option['attribute_id'] = $attr;
$option['value'][$this->getAttributeName($specific)][1] = $specific;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
我收到错误:“未定义默认选项值”
答案 0 :(得分:0)
您是否尝试使用'is_searchable' => true
代替'searchable' => true
?
答案 1 :(得分:0)
查看catalaog_eav_attribute。有一个'is_searchable'列,其类型为smallint
试试这个:
'is_searchable' => 1,
答案 2 :(得分:0)
参考错误:
"未定义默认选项值"
添加选项时:---------------------------------------> /
$option['value'][$this->getAttributeName($specific)][1] = $specific;
这里[0]是选项"默认管理值"并且[1]是"默认存储值"。
$option['value'][$this->getAttributeName($specific)][0] = $specific;
您的代码未使 $ specific 成为默认选项,但将 $ specific 设置为默认商店的值。