Magento - registry and current category

一世执手 提交于 2019-12-08 09:25:35

问题


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.



来源:https://stackoverflow.com/questions/10282847/magento-registry-and-current-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!