How To

How to Reindex Specific Products Programmatically in Magento 2?

Hello Magento Friends,

In this blog, I will explain How to Reindex Specific Products Programmatically in Magento 2.

Reindexing specific products in Magento 2 is a useful task when you want to quickly update product information in the search index, category listings, or other areas without reindexing the entire catalog. Reindexing specific products can help improve the performance and accuracy of your Magento 2 store.

Let’s check out the process for reindexing specific products programmatically in Magento 2.

Steps to Reindex Specific Products Programmatically in Magento 2:

Step 1: Create a custom PHP script like reindex_products.php

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

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
try {
 $productSkus = ['YOUR_SKU_1', 'YOUR_SKU_2'];
 $indexerFactory = $objectManager->get(\Magento\Indexer\Model\IndexerFactory::class);
 $indexerIds = ['catalog_product_attribute', 'cataloginventory_stock'];

 foreach ($productSkus as $productSku) {
     foreach ($indexerIds as $indexerId) {
  $indexer = $indexerFactory->create();
  $indexer->load($indexerId);
  $indexer->reindexRow($productSku);
     }
     echo "Reindexed product with SKU: $productSku\n";
 }
}
catch (\Exception $e){
    echo "****** Exception Throw ************* \n";
    echo $e->getMessage() . " \n";
}
?>

Reindex the Specific Product(s).

You can get loop through the product SKU(s) and trigger the reindexing process for each product.

Step 2: Execute the Script

Save the script and execute it via the command line using PHP:

php reindex_products.php

Here is the Screenshot:

Conclusion:

That’s it! You’ve successfully reindexed specific products in Magento 2. This process can help you keep your product data accurate and up-to-date without the need for a full reindex, which can be time-consuming on large catalogs.

To perform reindexing of the whole store check the below articles,

In case you find difficulty in performing reindexing  of specific products in Magento 2, you can share with me through the comment section and I will be quick to respond to you with the solution.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

1 hour ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

24 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 day ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

3 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

6 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago