I would like to reindex a single product after an update.
Now I use:
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'foobar');
// edit something
$product->save();
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$stockItem->setForceReindexRequired(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$stockItem,
Mage_CatalogInventory_Model_Stock_Item::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
$product
->setForceReindexRequired(true)
->setIsChangedCategories(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$product,
Mage_Catalog_Model_Product::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
But this doesn't work, any ideas?
PS:
$product->getId()
is existing and correct.
Best Answer
This works fine in Magento CE 1.6 and newer:
The available indexer codes can be viewed using the query:
In a native Magento 1.7 there are:
In Magento EE 1.13 it's different, there the indexer automatically picks up changed entities on each cron run (every minute).
UPDATE
The above answer is 100% correct anyway I think the below information can add something more.
If you need to change just few attributes values in a product and automatically update the relative index table you can use this function:
Mage::getSingleton('catalog/product_action')->updateAttributes();
if you want to manage the reindex by your own use the resourse model instead:
Mage::getResourceSingleton('catalog/product_action')->updateAttributes();
For example I use the following function to fast update only certain attributes in a product.
Note:
If you need to change the same attribute/values couple in a group of products you can pass the whole array of
product_ids