Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default select for product attribute - Magento

I've created a custom Yes/No attribute for products in Magento, using this code in a upgrade file:

$model = Mage::getModel('catalog/resource_eav_attribute');

$attributeData = array(
    'attribute_code' => 'custom_attribute',
    'frontend_label' => 'My custom attribute',
    'is_global' => '1',
    'frontend_input' => 'boolean',
    'backend_type' => 'int',
    'default_value' => null,
    'is_unique' => '0',
    'is_required' => '0',
    'is_configurable' => '1',
    'is_searchable' => '1',
    'is_visible_in_advanced_search' => '1',
    'is_comparable' => '1',
    'is_used_for_price_rules' => '0',
    'is_wysiwyg_enabled' => '1',
    'is_html_allowed_on_front' => '1',
    'is_visible_on_front' => '1',
    'used_in_product_listing' => '1',
    'used_for_sort_by' => '1',
    'is_filterable_in_search' => 1,
    'is_filterable' => 1,
    'source_model' => null,
    'option' => array(),
    'apply_to' => 'simple,configurable,bundle,grouped',
    'is_user_defined' => true,
    'global' => true,
);

$model->addData($attributeData);
$model->setEntityTypeId(
    Mage::getModel('eav/entity')->setType(Mage_Catalog_Model_Product::ENTITY)->getTypeId()
);
$model->save();

The code works. But I want the Yes option to be selected by default when I create a new product or I try to edit one. Now, only No is diplayed first. I try to change the defult_value to true, or 1, but it doesn't work.

like image 944
zuzuleinen Avatar asked Jan 25 '26 12:01

zuzuleinen


1 Answers

I've found that default_value is for the value of the option when you create a new product, and not for the products which don't have the attribute set. So I think in order to show 'Yes' on all products I should make a script to update all products with this attribute.

like image 91
zuzuleinen Avatar answered Jan 28 '26 23:01

zuzuleinen