Magento 1.7 – Default Value for Yes/No Dropdown Custom Product Attribute

attributesdefaultdropdown-attributemagento-1.7product-attribute

I install the attribute with the following script:

$installer = $this;
$installer->startSetup();

$installer->removeAttribute('catalog_product', 'customizableonly');
$installer->addAttribute('catalog_product', 'customizableonly', array(
        'group'                     => 'General',
        'input'                     => 'select',
        'type'                      => 'int',
        'label'                     => 'Customizable Only',
        'source'                    => 'eav/entity_attribute_source_boolean',
        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                   => 1,
        'required'                  => 0,
        'visible_on_front'          => 0,
        'is_html_allowed_on_front'  => 0,
        'is_configurable'           => 0,
        'searchable'                => 0,
        'filterable'                => 0,
        'comparable'                => 0,
        'unique'                    => false,
        'user_defined'              => false,
        'default'           => 0,
        'is_user_defined'           => false,
        'used_in_product_listing'   => true
));

$this->endSetup();

Also tried with $installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');

And then I am using the value of the attribute in some other code. But I always get null. I found out that, the attribute does not get set a default value. When I open a product – the dropdown shows No, but when I get its value in code it's null. If I just click the dropdown, simply set No and save the product – everything works.

How to overcome this?

Best Answer

Try to set default value as string

'default' => '0'

or empty

'default' => ''

Update

The default values are added when you add new product for old ones it not affects.

Try to fix that in Product management with mass action

Inside manage products, there is a action called “Update Attributes”. Select all the products that you want to update and then select Update Attributes and add all the new information in.