3

Is it possible to update product attributes without triggering reindex?

As known, the load()/setData()/save() scenario triggers reindex.

I've looked into 'updateAttributes' method in 'Mage_Catalog_Model_Product_Action' but I think it also triggers product reindex.

/**
     * Update attribute values for entity list per store
     *
     * @param array $productIds
     * @param array $attrData
     * @param int $storeId
     * @return Mage_Catalog_Model_Product_Action
     */
    public function updateAttributes($productIds, $attrData, $storeId)
    {
        $this->_getResource()->updateAttributes($productIds, $attrData, $storeId);
        $this->setData(array(
            'product_ids'       => array_unique($productIds),
            'attributes_data'   => $attrData,
            'store_id'          => $storeId
        ));

        // register mass action indexer event
        Mage::getSingleton('index/indexer')->processEntityAction(
            $this, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_MASS_ACTION
        );
        return $this;
    }

The the code under '// register mass action indexer event' seems to trigger indexer action.

2 Answers 2

8

I found the way to update the attributes of a product without triggering reindex.

The main idea is the following: if you do update operations on the Model of the product, reindex is triggered, but if you do those operations on the Resource, that specific operation is done without reindex.

For example:

/**
* This method updates product attributes then triggers reindex
*/
Mage_Catalog_Model_Product_Action->updateAttributes($productIds, $attrData, $storeId);

/**
* This method updates product attributes but doesn't trigger reindex
*/
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Action->updateAttributes($productIds, $attrData, $storeId);

where:

  • $productIds - an array with the products Ids
  • $attrData - an array having the keys as attribute name and values as attribute values
  • $storeId - store id of the product's assigned store (in case your product is assigned to multiple categories in different stores);

If the attribute is global Mage_Core_Model_App::ADMIN_STORE_ID can be used

Cheers!

2
  • I don't understand much your solution, If the attribute is global Mage_Core_Model_App::ADMIN_STORE_ID can be used . How can we use it ?
    – Tzu ng
    Commented Oct 10, 2012 at 9:22
  • 1
    @Vdt you can use that constant in the $storeId parameter Commented Dec 12, 2012 at 5:56
6

You could first stop the indexer like that:

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');

Then do your updates and afterwards reindex like that:

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');

Source: https://gist.github.com/2217520

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.