Magento API: rebuild indexes after adding new products

I am currently writing a script that allows you to import multiple products into magento.

$product = Mage::getModel('catalog/product'); $product->setSku($data['sku']); //etc etc $product->save(); 

The product is created perfectly, but it will not be displayed in my interface until I save it in the backend (without changing anything!) OR I rebuild the indexes in the backend.

I made a difference in the respective database tables to see what changes when I save the product and add these fields to my import script, but this had no effect. The imported product should be fine, as it appears when I manually rebuild the indexes through the backend.

Caching is completely disabled.

Now my question is: how do I rebuild indexes after importing my products?

+8
import api product magento
source share
2 answers

You can use such a model in the index module.

 $processes = Mage::getSingleton('index/indexer')->getProcessesCollection(); $processes->walk('reindexAll'); 

Since you need to rebuild all indexes, there are no filters associated with the collection. But you can filter the list of index processes by a set of parameters (code, last reindexed, etc.) using the addFieldToFilter($field, $condition) method.

Little suggestion

It would be great to set the indexes in manual mode when importing products, this will help you speed up the import process, because some of them observe a product saving event, so it takes some time. You can do it as follows:

 $processes = Mage::getSingleton('index/indexer')->getProcessesCollection(); $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL)); $processes->walk('save'); // Here goes your // Importing process // ................ $processes->walk('reindexAll'); $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME)); $processes->walk('save'); 
+34
source share

There are at least two circumstances that prevent indexing from overriding a product when it is saved.

One: the "Manual update" option in the index properties, which you will find in the "System Management", "Index Management" section. You must set it to β€œRefresh on Save” if you want the product to be indexed on save.

Two: the product flag setIsMassupdate, which is used, for example, in DataFlow batch import procedures to prevent the indexer from starting every time the product save method is called.

Hope this helps. Regards, Alessandro

+4
source share

Source: https://habr.com/ru/post/649785/


All Articles