Hello Magento Friends,
In this blog, I will explain How to Reindex Specific Products Programmatically in Magento 2.
Reindexing specific products in Magento 2 will be useful when you intend to update product information quite fast in search indexes or category listings or elsewhere. Reindexing specific products improves the performance and also accuracy for your store.
So let us check 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:
There you go! You successfully reindexed specific products in Magento 2. This is a process you might want to consider anytime you wish to keep your product data right up to speed without having to run a full catalog reindex, which can be really time-consuming on larger catalogs.
To do a complete reindexing of your store, please refer to the following articles:
- How to perform Reindexing Programmatically in Magento 2
- How to perform Reindexing from Command Line in Magento 2
If there are any difficulties in performing reindexing of specific products in Magento 2, then the concerns can be written in the comments below, and I will quickly respond back with a solution.
Happy Coding!