Magento 2 || Do a reindex by SKU - mpaz-redstage/magento-snippets GitHub Wiki

<?php
/**
 * @copyright: Copyright © 2017 Firebear Studio. All rights reserved.
 * @author   : Firebear Studio <[email protected]>
 */

namespace Redstage\FirebearFixes\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Catalog\Api\ProductRepositoryInterface;
use \Magento\Framework\App\Cache\Manager as Cache;

class ReindexProductISkuCommand  extends Command
{
    const PRODUCT_SKUS = 'skus';

    const INDEXER_LIST = [
        'catalog_category_product',
        'catalog_product_category',
        'catalogrule_rule',
        'catalog_product_attribute',
        'cataloginventory_stock',
        'catalog_product_price',
        'catalogrule_product',
        'catalogsearch_fulltext'
    ];

    protected $_indexerRegistry;

    protected $productRepository;

    protected $_cache;

    protected $_ids;

    public function __construct(
        IndexerRegistry $indexerRegistry,
        ProductRepositoryInterface $productRepository,
        Cache $cache,
        $name = null)
    {
        parent::__construct($name);
        $this->_indexerRegistry = $indexerRegistry;
        $this->_productRepository = $productRepository;
        $this->_cache = $cache;
        $this->_ids = [];
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('indexer:reindex:productSku')
            ->setDescription('Make a reindex of an array of products skus')
            ->setDefinition(
                [
                    new InputArgument(
                        self::PRODUCT_SKUS,
                        InputArgument::REQUIRED | InputArgument::IS_ARRAY,
                        'Space-separated list of product sku'
                    )
                ]
            );

        parent::configure();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("Reindexing products with the following SKUs:");
        $requestSkus = $input->getArgument(self::PRODUCT_SKUS);
        $requestSkus = array_filter(array_map('trim', $requestSkus), 'strlen');

        foreach($requestSkus as $sku) {
            $output->writeln("SKU: " . $sku);
            $product = $this->_productRepository->get($sku);
            $this->_ids[] = $product->getId();
        }
        $this->reindexProductSkus();
        $this->_cache->flush($this->_cache->getAvailableTypes());
        $output->writeln("Done");
    }

    public function reindexProductSkus() {
        foreach(self::INDEXER_LIST as $indexList) {
            $categoryIndexer = $this->_indexerRegistry->get($indexList);
            $categoryIndexer->reindexList(array_unique($this->_ids));
        }
    }
}