How can I add attribute to product properly?
-
06-10-2020 - |
题
I'm trying to add attribute to product entity. It seems that my upgrade script run, because module version is upgraded in core_resource
table.
However I can't find it at catalog attributes listing.
Upgrade script code:
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();
$installer->addAttribute(
Mage_Catalog_Model_Product::ENTITY,
'use_discount_label',
array(
'label' => 'Use discount label?',
'default' => '0',
'group' => 'Other',
'type' => 'int',
'input' => 'boolean',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'required' => false,
'user_defined' => false,
'unique' => false,
'filterable' => 1,
'is_configurable' => false,
'comparable' => true,
'filterable_in_search' => false,
'is_html_allowed_on_front' => false,
'searchable' => true,
'used_for_promo_rules' => false,
'used_for_sort_by' => true,
'used_in_product_listing' => true,
'visible' => false,
'visible_in_advanced_search' => false,
'visible_on_front' => false,
'wysiwyg_enabled' => false,
)
);
$installer->endSetup();
I'm using Magento EE 1.14.2.2.
解决方案
Your setup script looks correct, Try removing attribute before creating it. downgrade entry from core_resource
and run the script again.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->removeAttribute('catalog_product','use_discount_label');
$installer->startSetup();
$installer->addAttribute(
Mage_Catalog_Model_Product::ENTITY,
'use_discount_label',
array(
'label' => 'Use discount label?',
'default' => '0',
'group' => 'Other',
'type' => 'int',
'input' => 'boolean',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'required' => false,
'user_defined' => false,
'unique' => false,
'filterable' => 1,
'is_configurable' => false,
'comparable' => true,
'filterable_in_search' => false,
'is_html_allowed_on_front' => false,
'searchable' => true,
'used_for_promo_rules' => false,
'used_for_sort_by' => true,
'used_in_product_listing' => true,
'visible' => false,
'visible_in_advanced_search' => false,
'visible_on_front' => false,
'wysiwyg_enabled' => false,
)
);
$installer->endSetup();