How to get current catageory id?

I have a CMS page where I am going to display products with the following updated XML:

<reference name="content">
    <block type="catalog/product_list"  name="product_list" template="catalog/product/wholesale-list.phtml">
        <action method="setCategoryId"><category_id>191</category_id></action>
        <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
    </block>
</reference> 

I tried to get the identifier that I set in the layout, but there wasn’t such luck. I tried:

$_category = Mage::registry(‘current_category’);
$currentCategoryId= $_category->getId();

and

$layer = Mage::getSingleton(‘catalog/layer’);
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();

But none of these methods work. Does anyone know how I can get the id?

+5
source share
6 answers

I have not tried it, but maybe something like:

$this->getLayout()->getBlock('product_list')->getCategoryId()

This way you directly get the variable that you set in the Block object in XML.

Cheers,
JD

+4
source

I think this is the best way;)

Mage::registry('current_category')->getId();
+39
source

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

, CMS? , , CMS, , , Magento, :)

, → , , " ". " ". .

, , , -

<reference name="right">
        <remove name="right.permanent.callout" />
</reference>

To remove a block named right.permanent.callout from the layout in general. And if you just want to change the list of products to use your particular phtml file, you can do something like ...

<reference name="product_list">
        <action method="setTemplate"><template>catalog/product/wholesale.phtml</template></action>
</reference>

Perhaps you can use google to learn more about layouts.

+1
source

This works for me:

$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
0
source

This worked for me:

$currentCat = $this->getLayout()->getBlock('category.products')->getCurrentCategory();

Then you have the current category as an object, and you can get the identifier:

$currentCat->getId();
0
source

All Articles