9

I need to add to category a custom atribute , a select with 2 values:

  • 0 - "No"
  • 1 - "Yes"

I created a module and used this code in installation file:

$this->startSetup();
$this->addAttribute('catalog_category', 'top_brand', array(
    'group'                => 'General',
    'type'              => 'int',//can be int, varchar, decimal, text, datetime
    'backend'           => '',
    'frontend_input'    => '',
    'frontend'          => '',
    'label'             => 'Top Hersteller',
    'input'             => 'select', //text, textarea, select, file, image, multilselect
    'option' => array(
        'value' => array(

            'optionone'=> array(
                0 =>'No'),
            'optiontwo'=> array(
                0 =>'Yes')
        ),

    ),
    'default' => array(0),
    'class'             => '',
    // 'source'            => '',//this is necessary for select and multilelect, for the rest leave it blank
    'global'             => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,//scope can be SCOPE_STORE or SCOPE_GLOBAL or SCOPE_WEBSITE
    'visible'           => true,
    'frontend_class'     => '',
    'required'          => false,//or true
    'user_defined'      => true,
    'default'           => '',
    'position'            => 100,//any number will do
));
$this->endSetup();

Attribute appear in administration panel but value added in select for "No" is 3 and for "Yes" is 4. How to make value 0 and 1 ?

benmarks
  • 16,675
  • 4
  • 41
  • 108
user4157
  • 135
  • 2
  • 3
  • 7

3 Answers3

10

Try this:

$this->startSetup();
$this->addAttribute('catalog_category', 'top_brand', array(
    'group'                => 'General',
    'type'              => 'int',//can be int, varchar, decimal, text, datetime
    'backend'           => '',
    'frontend_input'    => '',
    'frontend'          => '',
    'label'             => 'Top Hersteller',
    'input'             => 'select', //text, textarea, select, file, image, multilselect
    'default' => array(0),
    'class'             => '',
    'source'            => 'eav/entity_attribute_source_boolean',//this is necessary for select and multilelect, for the rest leave it blank
    'global'             => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,//scope can be SCOPE_STORE or SCOPE_GLOBAL or SCOPE_WEBSITE
    'visible'           => true,
    'frontend_class'     => '',
    'required'          => false,//or true
    'user_defined'      => true,
    'default'           => '',
    'position'            => 100,//any number will do
));
$this->endSetup();

I added eav/entity_attribute_source_boolean as source for your attribute.

Marius
  • 197,939
  • 53
  • 422
  • 830
  • @Maurius thanks for the answer. But how would the source model eav/entity_attribute_source_boolean will work for multiselect option ? – Slimshadddyyy Apr 16 '14 at 05:16
  • 2
    @Vikram If you use that source model for a multiselect you will see a multiselect with 2 options. Yes/No. – Marius Apr 16 '14 at 06:31
  • @Marius i wan to Yes or No attribute where i have to add file or create new module for this – Magento 2 Dec 23 '15 at 07:54
2

Try to use below code to create top_brand attribute in category:

   $this->addAttribute( 'catalog_category', 'top_brand', array(
                'group' => 'General',
                'type' => 'tinyint',
                'backend' => '',
                'frontend' => '',
                'label' => 'Top Hersteller',
                'input' => 'boolean',
                'source' => 'eav/entity_attribute_source_boolean',
                'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '', 
                'unique' => false, 
            ) );
Bijal Bhavsar
  • 1,331
  • 10
  • 29
1

For adding the custom yes/no attribute in the category section please create the module and enter the following code.

 <?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'featured_product', array(
    'group'         => 'General Information',
    'input'         => 'select',
    'type'          => 'text',
    'label'         => 'Featured Product',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'eav/entity_attribute_source_boolean',
));

$this->endSetup();`

Please refer my tutorial as well.

http://www.pearlbells.co.uk/how-to-add-custom-attribute-dropdown-to-category-section-magento/ (yes/no) attribute

http://www.pearlbells.co.uk/how-to-add-custom-dropdown-attribute-to-magento-category-section/ (custom options)

Liz Eipe C
  • 1,286
  • 12
  • 17
user46226
  • 11
  • 1