2

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

8

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'
));
|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 '14 at 7:55
  • I don't understand, sorry. Maybe better expand the original post or write a new question? – Vinai May 30 '14 at 10:21
  • hi @Vinai using above code with default value set to '0' but on product page in admin it displays 'Yes' as default selected value (until I save it to No), it there a way in installation script that it shows 'No' by default? – Haris Feb 24 '17 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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