1

I have created a module to avoid an objectManager. In this I have

public function getCurrentCategory()
{        
    return $this->_registry->registry('current_category');
}

On my phtml file I call

$myBlock->getCurrentCategory()->getId();

I have full page cache enabled and created a custom submenu on the left sidebar. I want to make the font of the current category bold.

Everything works fine except when I click on an other category the category ID is not refreshed and the visited category remains selected until I manually clean the cache. How can I avoid caching from my function and make sure that each time the getCurrentCategory is refreshed properly?

1
  • this is due to your block is cached. you might have to hole punching
    – Arif Ahmad
    Sep 12, 2019 at 12:35

2 Answers 2

0

Please try with below way :

public function __construct(
    \Magento\Framework\App\Cache\Manager $cacheManager
) {
    $this->cacheManager = $cacheManager;
}
public function getCurrentCategory()
{        
    $this->cacheManager->flush($this->cacheManager->getAvailableTypes());
   $this->cacheManager->clean($this->cacheManager->getAvailableTypes());

    return $this->_registry->registry('current_category');
}

To put flush and clean cache in your code.

hope its work for you

3
  • Unfortunately it did not help. Thanks for your effort though
    – TheFrakes
    Oct 16, 2019 at 12:52
  • have another option goto menu layout and add menu block as cachable="false" Oct 16, 2019 at 12:54
  • I tried cacheable="false" but still no result. Maybe it does not work out of the design update within the category.
    – TheFrakes
    Oct 16, 2019 at 13:19
0

Try adding this method to your block class

public function getCacheKey()
{
    $storeId = $this->_storeManager->getStore()->getId();
    $catId = $this->getCurrentCategory()->getId();
    return "CATEGORY_SUBMENU_{$catId}_STORE_{$storeId}";
}

That will ensure every category gets an unique cached block instance. The point is that no 2 categories can have the same block in cache that's why we add category id and store id to the cache key.

Additionally you should add this method:

protected function getCacheLifetime()
{
    return 86400;
}

This will set cache lifetime of the block to 1 day (86400 seconds). Adjust as needed.

Not sure if this will fix your issue but it's worth trying :) I've had similar issues before and this has solved them most of the time.

Remember that disabling cache, flushing the cache constantly and cacheable="false" is never the solution.

3
  • What do you do with the cache key returned? Dec 15, 2022 at 17:21
  • @AdamLavery nothing. You just implement this method into your block class and let Magento handle the rest.
    – Zankar
    Dec 16, 2022 at 5:41
  • Thanks. Tried that but nothing doing. Added that to block file, cleaned & flushed cache but the function is never called (running a live debug session). We're using the related block to display other products in the same category. Some products are in two categories. After clearing cache and selecting the product from each category once it works fine, but select from the first category again and $registry->registry('current_category') returns null. Hence we fall back the first category the product belongs to. Dec 16, 2022 at 9:22

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.