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,
));
1

2 Answers 2

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(); 

?>
8
  • Can you provide info about in which attribute set this attribute will be added? Mar 2, 2017 at 13:16
  • It is added default attribute set
    – Abdul
    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');
    – Abdul
    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
    Mar 2, 2017 at 13:35
  • try this : $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('pim_id');
    – Abdul
    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,
));
2
  • Can you provide info about in which attribute set this attribute will be added? Mar 2, 2017 at 13:16
  • 1
    It will be added in all attribute set when it will created. Mar 2, 2017 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.