0

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))"

1 Answer 1

1

You can clean product cache using Object manager. It is not recommended nowadays though:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$cacheManager = $objectManager->get('\Magento\Framework\App\CacheInterface');
$cacheManager->clean('catalog_product_' . $productID);

If Varnish cache is on, it should be considered too. Check this answer.

But I use a little different approach for product prices which works fine:

  1. Setup prices update script where catalog_product_entity_decimal table is updated.

  2. Cron job runs script, reindexes and flushes cache using CLI

/usr/bin/php /home/[domain]/public_html/cron-prices.php && /usr/bin/php /home/[domain]/public_html/bin/magento indexer:reindex catalog_product_price catalog_product_flat && /usr/bin/php /home/[domain]/public_html/bin/magento cache:flush eav

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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