Magento - registry and current category

2019-06-08 22:51发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.