Magento

How to Magento 2 Reindex one Product Programmatically?

Written by Jigar Patel

Apr 01

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();
    }
    }
}

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP