How to Magento 2 Reindex one Product Programmatically?

Written by Jigar Patel

Apr 01, 2021

How to Magento 2 Reindex one Product Programmatically?

In this article we will learn how to Magento 2 Reindex one Product Programmatically. Indexer is the most important feature in Magento 2 Indexing.

For example, if more items and you change the price of a single item from $8.99 to $6.99. Magento must reindex the price change to display it on your storefront.

Reindexes automatically whenever one or more items change. Actions that trigger reindexing include price changes, creating catalog or shopping cart price rules, adding new categories, and so on. To optimize performance, Magento accumulates data into special tables using indexers. As the data changes, the indexed tables must be updated—or reindexed. Magento reindexes as a background process, and your store remains accessible during the processesCreatesingleItemReindex.php at /pub/singleItemReindex.php

<?php
use Magento\Framework\App\Bootstrap;

require '../app/bootstrap.php';
ini_set('display_errors', 1);

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$childIds = $objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->getChildrenIds('371009');//get configurable child item
$productIds = $childIds[0]; 

//$productIds = ['25206']; //pass product id
//list of indexers
$indexLists = ['catalog_category_product', 'catalog_product_category', 'catalog_product_attribute', 'cataloginventory_stock', 'inventory', 'catalogsearch_fulltext', 'catalog_product_price', 'catalogrule_product', 'catalogrule_rule'];

foreach ($indexLists as $indexList) {
    $categoryIndexer = $objectManager->get('Magento\Framework\Indexer\IndexerRegistry')->get($indexList);

    if (!$categoryIndexer->isScheduled()) {
    try {
        $categoryIndexer->reindexList(array_unique($productIds));
        echo $indexList . '<br>';
    } catch (\Exception $e) {
        echo $e->getMessage();
    }
    }
}

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP