The idea behind it, is some how keep a track of what's catalog rules are indexed. If i can run the index of a single rule in a module i can then save some logs and also do them in blocks of 10 for example


Extending upon this answer, here are some more details on how to reindex, how to get the IDs, and reindexing only what is needed.

ReindexAll can add a lot of overhead, especially on very large catalogs/sites.

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Indexer\Model\IndexerFactory $indexerFactory,
    \Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory
) {
    $this->eavConfigFactory = $eavConfigFactory;
    $this->eavSetupFactory = $eavSetupFactory;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->indexerFactory = $indexerFactory;
    $this->indexerCollectionFactory = $indexerCollectionFactory;

Then, when you want to use it:

public function ensureIndexersAreReady()
    $indexer = $this->indexerFactory->create();
    $indexerCollection = $this->indexerCollectionFactory->create();

    $ids = $indexerCollection->getAllIds();
    foreach ($ids as $id) {
        $idx = $indexer->load($id);
        if ($id == 'catalogrule_product') {
             $idx->reindexAll($id); // this reindexes all
             //$idx->reindexRow($id); // or you can use reindexRow according to your need
  • What is $idx? Magento\Indexer\Model\IndexerFactory? And what ID should be passed to reindexRow? A Rule ID? – Gianni Di Falco Jun 7 '18 at 10:08
  • Answer updated can you check please ? – user68116 Jun 7 '18 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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