Magento - registry and current category

2019-06-08 22:50发布

I have a question about Mage::registry and categories: I'm a on a category page, I retrieve current category by Mage::registry('current_category'). I've noticed that it works only for root categories, in fact if I visit a subcategory page I retrieve always the root category with Mage::registry('current_category'). So the question is: is something about backend configuration, cache or something else?

3条回答
疯言疯语
2楼-- · 2019-06-08 23:04

For all categories:-

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>

For Current Category

<?php $currentCategory = Mage::registry('current_category') ?>

Works for me.

查看更多
疯言疯语
3楼-- · 2019-06-08 23:06

If you are in a template (e.g. catalog/category/view.phtml) you can get the current category with

$this->getCurrentCategory();

If you are in a model, controller or else, try this (found here):

Mage::getModel('catalog/layer')->getCurrentCategory();

However, Mage::registry('current_category') is the normal way to go.

查看更多
We Are One
4楼-- · 2019-06-08 23:17

OOB, current_category is set in Mage_Catalog CategoryController::_initCategory() (ref here) and will always be equal to the category currently being viewed.

If your data is different then your app has non-standard functionality or you are seeing cached results.

查看更多
登录 后发表回答