How do I get the current catageory id?

33,876

Solution 1

Haven't tried this, but maybe something like:

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

This way you are directly getting the variable that you have set on the Block object in the XML.

Cheers,
JD

Solution 2

i think this is the best way ;)

Mage::registry('current_category')->getId();

Solution 3

Try below code

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

Solution 4

Have you considered updating the layout of the catalog page rather than making a CMS page? I suppose there are situations where you might prefer a CMS page, but you can update the layout of a category fairly easily, well about as easy as it gets in Magento, which isn't all that easy :)

Login to the admin backend, go to Catalog -> Manage Categories, then choose the category you want, then click the Custom Design tab. Notice the Custom Layout Updates field. This is where you can put your layout updates.

So for this category, if you didn't want to display a specific block you could do something like

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

Which would remove the block named right.permanent.callout from the layout altogether. And if you wanted to just change the product listing to use your specific phtml file you could do something like...

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

You can probably use google to find out more about how to layouts.

Solution 5

This works for me:

$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
Share:
33,876
Chris
Author by

Chris

Updated on July 05, 2022

Comments

  • Chris
    Chris almost 2 years

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

    <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 have tried getting the ID I set in the layout, but no such luck. I have tried:

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

    and

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

    But neither of these methods work. Does anyone know how I can get the ID?