Is it possible to get current category when Full Page Cache is enabled?
Catalog controller is not executed in this case, so the registry (current_category
key) is empty. All I can get is the root category
Thank开发者_如何学运维s
As one of the possible solution is to get category ID by given url. You have table of url rewrites and you have request string.
// Try to get category id directly from request
if (Mage::app()->getRequest()->getParam('id')) {
return Mage::app()->getRequest()->getParam('id');
}
// Try to get category id from request by rewrite request path
$aliases = Mage::app()->getRequest()->getAliases();
if ($aliases && is_array($aliases) && !empty($aliases) && $aliases['rewrite_request_path']) {
$urlRewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath($aliases['rewrite_request_path']);
if ($urlRewrite && $urlRewrite->getId()) {
return $urlRewrite->getCategoryId();
}
}
May be it's not beautiful solution but it worked well for me.
You need to get it via the layer:
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
Regards, Kenny
You are hole punching your block as indicated here:
I'm writing a module with random products list block, it's not affected by FPC by implementing hole punching
This means that your block is a stripped down version of Magento (see applyWithoutApp() method), so to access the current_category out of the registry, you'll need to register it in your Container in your hole punch module.
精彩评论