Magento Bulk Update Attributes

Hi guys, I skipped SQL from this for bulk update attributes using SKU / UPC

Launch EE1.10 FYI

I have all the rest of the code, but I'm not sure who / what / why is actually updating our attributes and could not find them, my logic is

  • Open CSV and take all skus and related attributes into a 2d array.
  • Parse SKU into entity_id
  • Take entity_id and attribute and run updates to completion
  • Spend the rest of the day from Friday.

Here is my (almost finished) code, I would GREATLY appreciate some help.

/** * FUNCTION: updateAttrib * * REQS: $db_magento * Session resource * * REQS: entity_id * Product entity value * * REQS: $attrib * Attribute to alter * */ 

See my answer for working code. Hope this helps someone in the Magento community.

+7
source share
2 answers

Although this may technically work, the code you wrote is the last way you should do this.

In Magento, you really should use the models provided by the code and not write database queries yourself.

In your case, if you need to update attributes for 1 or many products, you can do it very quickly (and quite safely).

If you look at: /app/code/core/Mage/Adminhtml/controllers/Catalog/Product/Action/AttributeController.php, you will find that this controller is designed to quickly update several products.

If you look in the saveAction () function, you will find the following line of code:

 Mage::getSingleton('catalog/product_action') ->updateAttributes($this->_getHelper()->getProductIds(), $attributesData, $storeId); 

This code is responsible for updating all the product identifiers you want, and only the changed attributes for any single store at a time.

The first parameter is basically an array of product identifiers. If you only want to update one product, just put it in an array.

The second parameter is an array containing the attributes that you want to update for these products. For example, if you want to update the price to $ 10 and weight to 5, you must pass the following array:

 array('price' => 10.00, 'weight' => 5) 

Then, finally, the third and last attribute is the identifier of the store with which you need these updates. Most likely, this number will be equal to 1 or 0.

I would play around with this function call and use this instead of writing and supporting your own database queries.

+24
source

The general update request will look like this:

 UPDATE catalog_product_entity_[backend_type] cpex SET cpex.value = ? WHERE cpex.attribute_id = ? AND cpex.entity_id = ? 

To find the [backend_type] associated with the attribute:

 SELECT  backend_type FROM  eav_attribute WHERE entity_type_id =  (SELECT    entity_type_id  FROM    eav_entity_type  WHERE entity_type_code = 'catalog_product') AND attribute_id = ? 

You can get more information from the following blog article:
http://www.blog.magepsycho.com/magento-eav-structure-role-of-eav_attributes-backend_type-field/

Hope this helps you.

0
source

All Articles