Magento: Programmatically Modify Attribute Configuration

Today I was asked programmatically modify a product attribute so that it was no longer searchable, and make a different attribute searchable instead. I wrote up the code and put it on our database updater module to run as an upgrade script, but you could put it anywhere you want. To modify an attribute, you can do the following:

// Method 1
$model = Mage::getModel('catalog/resource_eav_attribute');
$model->loadByCode(4, 'color');
if ($model) {
// Method 2
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','color');
if ($attributeId) {
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);

You can see I used the setIsSearchable() magic method to set the the ‘is_searchable’ for the color attribute to 0, which is “No”. You can change whatever you want and then save it and you are good to go.

If you want to know what options you can change, load up the attribute, and log/output $attribute->getData(). It will show you all the current settings.

This entry was posted in Magento. Bookmark the permalink.

3 Responses to Magento: Programmatically Modify Attribute Configuration

  1. Magento Development India says:

    Great article! I really like your blog and thanks for tutorials.

  2. Also you can use Mage_Eav_Model_Entity_Setup::update Attribute() technique.

  3. Snowcore says:

    Also you can use Mage_Eav_Model_Entity_Setup::updateAttribute() method.