Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active April 28, 2020 09:03
Show Gist options
  • Save evgv/b44601a9daf14c7e3bb9369bdcba1ba4 to your computer and use it in GitHub Desktop.
Save evgv/b44601a9daf14c7e3bb9369bdcba1ba4 to your computer and use it in GitHub Desktop.
Magento. Update attribute via install/upgrade script.

Update attribute via install/upgrade script

In example below I upgraded is_visible_on_front and used_in_product_listing from false to true for two attributes namely flavor and unit_count. These attributes are product attributes and refer to Mage_Catalog_Model_Product::ENTITY or catalog_product. For update attribute params we are need the: entity id of attribute, id of attribute, field name what we wont to change, new field value and sort order. Function what we will use is updateAttribute from class Mage_Eav_Model_Entity_Setup.

Install/upgrade script in my case this is the mysql4-upgrade-1.1.4-1.1.5.php

    /* @var $installer Mage_Catalog_Model_Resource_Setup */
    $installer = $this;
    $installer->startSetup();

    $entityTypeId = $installer->getEntityTypeId(Mage_Catalog_Model_Product::ENTITY);

    $attributeCodes = array('flavor', 'unit_count');

    foreach ($attributeCodes as $attributeCode) {
        $attributeId = $installer->getAttributeId($entityTypeId, $attributeCode);

        $installer->updateAttribute($entityTypeId, $attributeId, 'is_visible_on_front', true); 
        $installer->updateAttribute($entityTypeId, $attributeId, 'used_in_product_listing', true); 
    }

    $installer->endSetup();

Update attribute function Mage_Eav_Model_Entity_Setup::updateAttribute()

      /**
     * Update Attribute data and Attribute additional data
     *
     * @param mixed $entityTypeId
     * @param mixed $id
     * @param string $field
     * @param mixed $value
     * @param int $sortOrder
     * @return Mage_Eav_Model_Entity_Setup
     */
    public function updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null)
    {
        $this->_updateAttribute($entityTypeId, $id, $field, $value, $sortOrder);
        $this->_updateAttributeAdditionalData($entityTypeId, $id, $field, $value);
        return $this;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment