zuzuleinen
zuzuleinen

Reputation: 2634

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.

Upvotes: 3

Views: 3101

Answers (3)

zuzuleinen
zuzuleinen

Reputation: 2634

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.

Upvotes: 1

Swapna
Swapna

Reputation: 401

I think you need to update the attribute. Lets say 'custom_attribute' is the attribute code of your new attribute and 'default value' is the one which you want to change/update, then you add the below code as a new upgrade script.

    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();

    $aAttribute = $installer->getAttribute('catalog_product', 'custom_attribute');
    $aAttribute['default_value'] = '1';

    $installer->updateAttribute('catalog_product', 'custom_attribute', $aAttribute);
    $installer->endSetup();

I hope this works.

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53565

In order to add an attribute you should go to the folder:

/var/www/html/<YOUR APP>/app/code/core/Mage/Catalog/etc

and edit the file: config.xml, go to the section:

<product> 
<collection> 
<attributes>

and add the required attributes. Next, go to the folder:

/var/www/html/<YOUR APP>/app/design/frontend/base/<YOUR THEME>/template/catalog/product

and edit the file new.phtml and whenever you need to use the attribute call:

<?php echo $_product→getAttributeName();?>

For example, if your attribute is called size, use:

<?php echo $_product→getSize();?>

Upvotes: 0

Related Questions