0

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?

RAS
  • 8,100
  • 16
  • 64
  • 86
Luke
  • 1,794
  • 10
  • 43
  • 70

3 Answers3

7

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.

Black
  • 18,150
  • 39
  • 158
  • 271
SebiF
  • 509
  • 5
  • 16
4

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.

benmarks
  • 23,384
  • 1
  • 62
  • 84
1

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.

Noor
  • 144
  • 2
  • 5