5

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
asked Mar 2, 2017 at 12:59
1

3 Answers 3

7

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
8
  • Can you provide info about in which attribute set this attribute will be added? Commented Mar 2, 2017 at 13:16
  • It is added default attribute set Commented Mar 2, 2017 at 13:18
  • 2
    If 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'); Commented 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? Commented Mar 2, 2017 at 13:35
  • try this : $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('pim_id'); Commented Mar 3, 2017 at 5:38
0

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
2
  • Can you provide info about in which attribute set this attribute will be added? Commented Mar 2, 2017 at 13:16
  • 1
    It will be added in all attribute set when it will created. Commented Mar 2, 2017 at 13:17
0

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');
answered Oct 19, 2023 at 1:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.