Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current catageory id?

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?

like image 816
Chris Avatar asked Mar 31 '11 22:03

Chris


People also ask

How do I find the current category ID in WooCommerce?

The first thing you need to do is log into your WordPress Dashboard and go to WooCommerce->Products->Categories. Now that you know how to find the current category id in WooCommerce, you can use this information to create custom WooCommerce shortcodes or show different products on different pages of your store.

How do I get the current category slug in WordPress?

php using the following code so the category-slug is displayed inside the string based on the current category: $cat = get_query_var('cat'); $yourcat = get_category ($cat);


3 Answers

i think this is the best way ;)

Mage::registry('current_category')->getId();
like image 165
Ansyori Avatar answered Oct 16 '22 10:10

Ansyori


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

like image 43
Jonathan Day Avatar answered Oct 16 '22 09:10

Jonathan Day


Try below code

 Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
like image 24
Rohit Goel Avatar answered Oct 16 '22 10:10

Rohit Goel