Magento: Bulk Update Product Attribute (using sql file or from a controller)


After creating a New Magento Module to add special functionality to catalog products I needed to set the value for this product to all downloadable products that were already created because this is a required attribute and the site would not function correctly with out it. So here is how you can update an attribute(s) for a given set of products. Please note that this will update a text attribute. If you are trying to update a select or multiselect attribute you may need to format you values differently.

If you can not figure out the exact format you need you can test it by doing a bulk attribute update from the admin and edit the file for Mage_Catalog_Model_Product_Action class and in the updateAttributes method just print the value for $attrData. Then mimic this in you update script.

01.//Get the product Ids you want to affect. For me it was all downloadable products
02.$downloadableProductIds = Mage::getResourceModel('catalog/product_collection')
03.->addAttributeToFilter('type_id', Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE)
06.//Now create an array of attribute_code => values
07.$attributeData = array('my_attribute_code' => 'my_attribute_value);
08.//Set the store to affect. I used admin to change all default values
09.$storeId = 0; //A.K.A Admin
10.//Now Update the attribute(s) for the given products.
12.->updateAttributes($downloadableProductIds, $attributeData, $storeId);

Comments are closed.