1

I can successfully updated the inventory qty in my custom module using the following lines of code. But the old value is still cached and displayed. I have already added cacheable="false" in the layout xml .

But it does not work. How to flush the cache after I have updated the value? What is the proper way to do this?

I now disable the full page cache for a workaround.

$stockItem = $this->stockRegistry->getStockItem($product->getId());
$stockItem->setData('qty',$qty-1);
$stockItem->save();
$product->save();
  • Have u do indexing after stock update. – Amit Bera Oct 19 '16 at 8:33
  • No. Do I need to run re-index every time I update the inventory qty? If so, I doubt that it works when I disabled the page cache. – Paul Oct 19 '16 at 9:17
  • 1
    Don't add cacheable=false on blocks because it's very bad, it won't let that page to be full page cached anymore. Don't use it. And if you have Indexer set to update by schedule than you need to add Magento 2 cron to crontab or whatever. MView will update cache and stock indexers for you (in cron). Or you can run the reindex command via cli .. – obscure Oct 22 '16 at 22:08
  • this may help magento.stackexchange.com/a/131837/46716 – Gediminas Jan 4 '18 at 17:58

Your Answer

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

Browse other questions tagged or ask your own question.