koenig
koenig

Reputation: 101

How set a default Value with EAV AddAttribute

I want to set up an new attribute-set to my products in magento. This attribute should be type of selection from some options.

$installer->addAttribute('catalog_product', 'reserve', array(
    'backend_label'     => 'Attribute Reserve',
    'type'              => 'varchar',
    'input'             => 'select',
    #'backend'          => 'eav/entity_attribute_source_boolean',
    'frontend'          => '',
    'source'            => '',
    #'default'          => 1,
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'visible_in_advanced_search' => false,
    'unique'            => false,
    'option' => array(
        'value' => array( 
            'optionone' => array( 'O' ),
            'optiontwo' => array( 'P' ),
            'optionthree' => array( 'Kein Angabe' ),
        )
    ),
));

How can I set optionthree to default value?

Upvotes: 3

Views: 9807

Answers (3)

f0xdx
f0xdx

Reputation: 1469

Had the same problem. My solution:

$installer->addAttribute('catalog_product', 'reserve', array(
    'backend_label'     => 'Attribute Reserve',
    'type'              => 'int',
    'input'             => 'select',
   #'backend'           => 'eav/entity_attribute_source_boolean',
    'frontend'          => '',
    'source'            => 'eav/entity_attribute_source_table',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'visible_in_advanced_search' => false,

    'option' => array(
        'value' => array( 
            'optionone'   => array( 'O' ),
            'optiontwo'   => array( 'P' ),
            'optionthree' => array( 'Kein Angabe' ),
        )
    ),
));

Notice the different type (int instead of varchar) and source (eav/entity_attribute_source_table). This is the way Magento represents typical select attributes. Now you can set the default value like this:

 $model = Mage::getModel('eav/entity_attribute')
     ->load($installer->getAttributeId('catalog_product', 'reserve'));
 $model
     ->setDefaultValue($model->getSource()->getOptionId('Keine Angabe'))
     ->save();

Upvotes: 7

Knowledge Craving
Knowledge Craving

Reputation: 7995

Please use this script:-

$installer->addAttribute('catalog_product', 'reserve', array(
    'backend_label'     => 'Attribute Reserve',
    'type'              => 'varchar',
    'input'             => 'select',
    #'backend'          => 'eav/entity_attribute_source_boolean',
    'frontend'          => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'visible_in_advanced_search' => false,

    'option' => array(
        'value' => array( 
            'optionone'   => array( 'O' ),
            'optiontwo'   => array( 'P' ),
            'optionthree' => array( 'Kein Angabe' ),
        )
    ),
    /**
     * This will set the default values,
     * as "array" data type is being used to set proper default value
     */
    'default' => array(
        'optionthree'
    ),
));

Hope it helps.

Upvotes: 4

Sarath Tomy
Sarath Tomy

Reputation: 504

Navigate to Catalog >Manage Attributes to create new attribute and manage attribue stes to create new attribute set.enter image description here

Please check the screenshot

Upvotes: -1

Related Questions