Is there a way to add a custom Attribute (DB-Column, whatever) to product attribute sets?
I tried the following but it I get the error 'Wrong Entity ID':
/** @var Mage_Catalog_Model_Resource_Setup $this */
$this->addAttribute('eav/entity_attribute_set', 'pim_id', array(
'group' => 'General Information',
'input' => 'text',
'type' => 'text',
'label' => 'PIM ID',
'backend' => '',
'visible' => false,
'required' => false,
'is_unique' => true,
'visible_on_front' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
Prasanta Hatui
1,8311 gold badge11 silver badges16 bronze badges
-
2check this link : magento.stackexchange.com/questions/46358/…Prasanta Hatui– Prasanta Hatui2017年03月02日 13:07:25 +00:00Commented Mar 2, 2017 at 13:07
3 Answers 3
Try this
<?php
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', 'pim_id', array(
'group' => 'General',
'label' => 'PIM ID',
'input' => 'text',
'type' => 'varchar',
'required' => 0,
'visible_on_front'=> 1,
'filterable' => 0,
'searchable' => 0,
'comparable' => 0,
'user_defined' => 1,
'is_configurable' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'note' => '',
));
$installer->endSetup();
?>
Dropdown input with options
<?php
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', 'addtocart_settings', array(
'group' => 'General',
'label' => 'Add To Cart Settings',
'input' => 'select',
'type' => 'varchar',
'required' => 0,
'visible_on_front' => false,
'filterable' => 0,
'filterable_in_search' => 0,
'searchable' => 0,
'used_in_product_listing' => true,
'visible_in_advanced_search' => false,
'comparable' => 0,
'user_defined' => 1,
'is_configurable' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'option' => array('values' => array('Default', 'Available at authorized dealer','Not available yet')),
'note' => ''
));
$installer->endSetup();
?>
answered Mar 2, 2017 at 13:14
Abdul
9,7291 gold badge22 silver badges44 bronze badges
-
Can you provide info about in which attribute set this attribute will be added?Jaimin Sutariya– Jaimin Sutariya2017年03月02日 13:16:57 +00:00Commented Mar 2, 2017 at 13:16
-
It is added default attribute setAbdul– Abdul2017年03月02日 13:18:08 +00:00Commented Mar 2, 2017 at 13:18
-
2If need to add your custom attribute set then add code $installer->setAttributeSetId('catalog_product','Custom Attribute Set Name','General','pim_id'); for eg.$installer->setAttributeSetId('catalog_product','Watches','General','pim_id');Abdul– Abdul2017年03月02日 13:22:53 +00:00Commented Mar 2, 2017 at 13:22
-
This works nicely. But unfortunatelly I can't filter by this field or is
Mage::getModel(Mage_Catalog_Model_Product::class, ...)the wrong class?Felix– Felix2017年03月02日 13:35:07 +00:00Commented Mar 2, 2017 at 13:35 -
try this : $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('pim_id');Abdul– Abdul2017年03月03日 05:38:30 +00:00Commented Mar 3, 2017 at 5:38
Try this :
/** @var Mage_Catalog_Model_Resource_Setup $this */
$this->addAttribute('catalog_product', 'pim_id', array(
'group' => 'General Information',
'input' => 'text',
'type' => 'text',
'label' => 'PIM ID',
'backend' => '',
'visible' => false,
'required' => false,
'is_unique' => true,
'visible_on_front' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
answered Mar 2, 2017 at 13:14
Ashish Jagnani
6,3047 gold badges36 silver badges71 bronze badges
-
Can you provide info about in which attribute set this attribute will be added?Jaimin Sutariya– Jaimin Sutariya2017年03月02日 13:16:40 +00:00Commented Mar 2, 2017 at 13:16
-
1It will be added in all attribute set when it will created.Ashish Jagnani– Ashish Jagnani2017年03月02日 13:17:31 +00:00Commented Mar 2, 2017 at 13:17
Ref this answer,
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
the $installer would not update the additional table catalog_eav_atttribute. That means fields such as visible, searchable, filterable, etc. would all be set to the default value. Instead, it should be
$installer = new Mage_Catalog_Model_Resource_Setup('catalog_setup');
default