Reindexing Specific Products in Magento 2
In this article, we’ll create a new command for reindexing specific products in Magento 2. With this command, you can reindex single or multiple products by ID or SKU.
As we all know Magento 2 provides an indexing command by default but when we run that command it’ll reindex all the products. If we have a small catalog then it’ll be fine but if we have a big catalog then it’ll task more time to complete the process. So, to overcome this issue we’ll create new command which will help you reindex specific products.
Let's get started!!
To create a custom command you have to follow the simple steps which are listed below.
Before we start, we need to create Devhooks folder as a Namespace at app/code/ and create a Module folder SpecificProductIndexing at app/code/Devhooks/ location.
Step 1: Create a module.xml file at app/code/Devhooks/SpecificProductIndexing/etc/ and paste the below code.
Note: Don't forgot to replace the Devhooks_SpecificProductIndexing
with your Namespace and Module Name if it's different.
saveCopyzoom_out_map<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Devhooks_SpecificProductIndexing" setup_version="1.0.0">
</module>
</config>
Step 2: Create registration file registration.php at app/code/Devhooks/SpecificProductIndexing/ and paste the below code.
saveCopyzoom_out_map<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Devhooks_SpecificProductIndexing', __DIR__ );
Step 3: Create a di.xml at app/code/Devhooks/SpecificProductIndexing/etc/ and paste the below code.
saveCopyzoom_out_map<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="Customcommand" xsi:type="object">Devhooks\SpecificProductIndexing\Console\Customcommand</item>
</argument>
</arguments>
</type>
</config>
Step 4: Create a Customcommand.php at app/code/Devhooks/SpecificProductIndexing/Console/ and paste the below code.
saveCopyzoom_out_map<?php
namespace Devhooks\SpecificProductIndexing\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Customcommand extends Command
{
const SKU = 'sku';
const ID = 'id';
protected $indexerRegistry;
protected $productCollection;
public function __construct(
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
\Magento\Catalog\Model\Product $productCollection
) {
$this->indexerRegistry = $indexerRegistry;
$this->productCollection = $productCollection;
parent::__construct();
}
protected function configure()
{
$this->setName('indexer:reindex:specific');
$this->setDescription('Reindex Specific Product.');
$this->addOption(
self::SKU,
null,
InputOption::VALUE_REQUIRED,
'Reindex specific product by SKU'
);
$this->addOption(
self::ID,
null,
InputOption::VALUE_REQUIRED,
'Reindex specific product by ID'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$indexList = [
'catalog_category_product',
'catalog_product_category',
'catalog_product_attribute',
'cataloginventory_stock',
'inventory',
'catalogsearch_fulltext',
'catalog_product_price',
];
if ($input->getOption(self::SKU) || $input->getOption(self::ID)) {
foreach ($indexList as $index) {
$Indexer = $this->indexerRegistry->get($index);
if($input->getOption(self::SKU)){
$productIds = explode(',', $input->getOption(self::SKU));
}else{
$productIds = explode(',', $input->getOption(self::ID));
}
foreach ($productIds as $id) {
if($input->getOption(self::SKU)){
$id = $this->getProductIdBySku($id);
}
$Indexer->reindexList([$id]);
}
$output->writeln('<info>'.$Indexer->getTitle().' index has been rebuilt successfully</info>');
}
}else{
$output->writeln('<error>Please add --id=1 or --sku=24-MB01,24-MB04</error>');
$output->writeln('<comment>For Ex: php bin/magento indexer:reindex:specific --sku=24-MB01</comment>');
}
} catch (\Exception $e) {
echo $e->getMessage();
}
}
public function getProductIdBySku($sku) {
return $productId = $this->productCollection->getIdBySku($sku);
}
}
Step 5: Open Command line in folder root of magento and run the below commands.
saveCopyzoom_out_mapphp bin/magento setup:upgrade php bin/magento cache:clean
Finally, our new command is created. To confirm that run the below command to check the Magento command list and find your command in a list under the indexer section.
saveCopyzoom_out_mapphp bin/magento
How to use this command?
In the above code, you can see we've created a new command indexer:reindex:specific
with 2 options --id
and --sku
.
You can use that commad like below examples.
Reindex single product by ID/SKU
saveCopyzoom_out_mapphp bin/magento indexer:reindex:specific --id=1
saveCopyzoom_out_mapphp bin/magento indexer:reindex:specific --sku=24-MB01
Reindex multiple products by IDs/SKU
saveCopyzoom_out_mapphp bin/magento indexer:reindex:specific --id=1,2
saveCopyzoom_out_mapphp bin/magento indexer:reindex:specific --sku=24-MB01,24-MB04
You need to add IDs/SKU by ,
seprate if you want to reindex multiple products.
That's it.
We have successfully created new command for reindex specific product by Ids/SKU. Also we've tested with above commands.
We hope this article helped you to know How to reindex specific product In Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this post with your team.
Review other articles maybe it'll help you too.
- How To Create Magento 2 Module
- How to Add Image Uploader on Store Configuration in Magento 2
- How to Add Custom Select/MultiSelect Options on Store Configuration in Magento 2
- How to Add Dynamic Row on Store Configuration in Magento 2
- How to Add Date & time Picker on Store Configuration in Magento 2
- How to Add Link on Store Configuration Fields Comment in Magento 2
- How to Use Javascript Mixins in Magento 2
- How to Add Custom Validation Rule in Magento 2
- How to Get Product Collection
- How to Setup Magento 2 PWA Studio
- How to Get Current Category
- How to Get Current Product
- Get Product Collection with Filters
- How to Create And Use Plugin In Magento 2