1

how to programmatically reindex product after update ex. price/stock quantity using Magento ver. 2.3.2 API via Swagger? I do application in C# which sync my system with Magento. I have a lot of article, so I think migrate ex. Pirce using catalogBasePriceStorage. But after that, i must manually reindex from admin panel. s there any way to execute reindex from Magento Api?

2 Answers 2

1

Create a custom Rest API in magento 2 and use below code with magento

    <?php
namespace Vendor\module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
 protected $_pageFactory;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
     \Magento\Indexer\Model\IndexerFactory $indexerFactory,
        \Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory,
    \Magento\Framework\View\Result\PageFactory $pageFactory)
{
    $this->_pageFactory = $pageFactory;
    $this->_indexerFactory = $indexerFactory;
    $this->_indexerCollectionFactory = $indexerCollectionFactory;
    return parent::__construct($context);
}

public function execute()
{
   $indexerCollection = $this->_indexerCollectionFactory->create();
  $indexerFactory = $this->_objectManager->get('Magento\Indexer\Model\IndexerFactory');
$indexerIds = array(
    'catalog_category_product',
    'catalog_product_category',
    'catalog_product_price',
    'catalog_product_attribute',
    'cataloginventory_stock',
    'catalogrule_product',
    'catalogsearch_fulltext',
);
foreach ($indexerIds as $indexerId) {
    echo " create index: ".$indexerId."\n";
    $indexer = $indexerFactory->create();
    $indexer->load($indexerId);
    $indexer->reindexAll();
}
}
}
0
$indexerFactory = $this->_objectManager->get('Magento\Indexer\Model\IndexerFactory');
$indexerIds = array(
    'catalog_category_product',
    'catalog_product_category',
    'catalog_product_price',
    'catalog_product_attribute',
    'cataloginventory_stock',
    'catalogrule_product',
    'catalogsearch_fulltext',
);
foreach ($indexerIds as $indexerId) {
    echo " create index: ".$indexerId."\n";
    $indexer = $indexerFactory->create();
    $indexer->load($indexerIds);
    $indexer->reindexAll();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.