Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I have used below code to generate a custom select attribute.

$installer->addAttribute('catalog_product', "best_seller_flag", array(
    'type'       => 'int',
    'group'      => 'Flags',
    'input'      => 'select',
    'label'      => 'Best seller flag',
    'default'    => '',
    'sort_order' => 2,
    'required'   => false,
    'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'backend'    => 'eav/entity_attribute_backend_array',
    'option'     => array (
        'values' => array(
            0 => 'No',
            1 => 'Yes'
        )
    ),
));

$installer->addAttribute('catalog_product', /* another attribute */);

$installer->addAttribute('catalog_product', /* another attribute */);

The attribute was created successfully but by default "No" option is not selected. It is showing in admin panel as shown in the below image:

enter image description here


When I inspect the element, I can see the following code (related to a single attribute)

<select class=" select" name="product[best_seller_flag]" id="best_seller_flag">
    <option selected="selected" value=""></option>
    <option value="156">No</option>
    <option value="157">Yes</option>
</select>

As you can see in above code, the values of options are not 0 and 1, instead they are some numbers like 156 and 157.

I think this is the main reason to why the 'default' => '' is not working. (even tried 'default' => '0')

Please help

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Using the boolean source model instead of the table source model you can easily define the default 0:

$installer->addAttribute('catalog_product', "best_seller_flag", array(
    'type'       => 'int',
    'group'      => 'Flags',
    'input'      => 'select',
    'label'      => 'Best seller flag',
    'default'    => '0',
    'sort_order' => 2,
    'required'   => false,
    'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'source'     => 'eav/entity_attribute_source_boolean'
));
share|improve this answer
    
yup this worked.. thanks :) a small doubt.. can I write my own html in this properties of custom attribute? if yes, what property I should use? –  Mr_Green May 30 at 7:55
    
I don't understand, sorry. Maybe better expand the original post or write a new question? –  Vinai May 30 at 10:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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