I am having some issues doing this programmatically - I have tried a few methods but the price before the change remains.
Basically, I want to reindex pricing for a select number of products, and then clear cache so the new price will display immediately.
The code reindexes individual products - but cache remains uncleared (old price still visible on category page/product page):
$ids = array_unique($ids);
/* \Magento\Indexer\Model\Indexer */
$indexer = $this->_indexer->load('catalog_product_price');
if (!$indexer->isScheduled()) {
$indexer->reindexList($ids);
}
foreach ($ids as $id) {
$product = $this->_product->loadByAttribute('entity_id', $id);
$identities = $product->cleanCache();
foreach ($identities as $identity) {
/* \Magento\Framework\Indexer\CacheContext */
$this->_cacheContext->registerEntities($identities, $ids);
/* \Magento\Framework\Event\ManagerInterface */
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $this->_cacheContext]);
}
I have gone through a few variations of the above, none with the desired result.
This code causes an error:
/* \Magento\Catalog\Model\Indexer\Product\Price */
$this->_priceIndexer->executeList($ids)
Error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '758-0- 1' for key 'PRIMARY', query was: INSERT INTO `catalog_product_index_price_tmp` SELECT `catalog_product_index_price_final_tmp`.`entity_id`, `catalog_product_index_price_final_tmp`.`customer_group_id`, `catalog_product_index_price_final_tmp`.`website_id`, `catalog_product_index_price_final_tmp`.`tax_class_id`, `catalog_product_index_price_final_tmp`.`orig_price` AS `price`, `catalog_product_index_price_final_tmp`.`price` AS `final_price`, `catalog_product_index_price_final_tmp`.`min_price`, `catalog_product_index_price_final_tmp`.`max_price`, `catalog_product_index_price_final_tmp`.`tier_price` FROM `catalog_product_index_price_final_tmp` WHERE (entity_id in (139, 1204, 748, 1026, 1027, 758, 1030, 1031, 1043, 788, 927, 928))"