I have get the current product category id on product details page. I have used some method like :

$_product = Mage::getModel('catalog/product')->load(prodId);
$ids = $_product->getCategoryId();
$cat = Mage::getModel('catalog/product')->setId($ids);

but its not work as i want.

$products = Mage::getResourceModel('reports/product_collection')

but its return some times this error

 Fatal error: Call to a member function getId() on a non-object in /var/www/html/app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php on line 719 

enter image description here

  • current product category id on product details means that category from where are goes to product details page?
    – Amit Bera
    Commented Mar 29, 2016 at 7:14
  • sorry its my mistake. i have set ->addCategoryFilter($cat) but not fixed result.
    – Ajay Patel
    Commented Mar 29, 2016 at 7:28
  • Try $cat = Mage::getModel('catalog/category')->load(array_pop($ids)); Commented Mar 29, 2016 at 7:32
  • Possible you need another filter $products->addFieldToFilter('category_id', array('in' => $catIds)); Commented Mar 29, 2016 at 7:36
  • here Category_ids empty @SiarheyUchukhlebau.
    – Ajay Patel
    Commented Mar 29, 2016 at 7:37

5 Answers 5


Because 1 product can stored in multi categories, so when call

$categoryIds = $_product->getCategoryIds();

it will a array.

foreach($categoryIds as $id) {
  $cat = Mage::getModel('catalog/category')->load($id);

You can use below code:

$categoryIds = $_product->getCategoryIds();        
if(count($categoryIds) ){
    $firstCategoryId = $categoryIds[0];
    $_category = Mage::getModel('catalog/category')->load($firstCategoryId);

    echo $_category->getName();
    echo $_category->getId();
  • Products can be attached to multiple categories. In your situation, when you visit a product page referred from a category page, your session has category information. But if you visit directly product page, Magento can not know which category you came from, so it can not show you a specific category, because your product can have multiple categories. But in your situation, if your products are attached only one category, you can use above code, it shows first category name of product;
    – Kaka
    Commented Mar 29, 2016 at 7:12
  • @Kaka It will work if you visit directly product page.I have checked it
    – Arunendra
    Commented Mar 29, 2016 at 7:20
  • please refer updated question.
    – Ajay Patel
    Commented Mar 29, 2016 at 7:25
  • @Arunendra If so, you will get multiple category id if this product attached in multiple category, right ? or you will get single category id ??
    – Kaka
    Commented Mar 29, 2016 at 7:57
  • @Arunendra could you pls help me with my own post magento.stackexchange.com/q/284557/57334
    – zus
    Commented Aug 6, 2019 at 12:37

Try this:

//This will get the info of current product.

$product = Mage::registry('current_product'); 

$prodID = $product->getId();

$_product = Mage::getModel('catalog/product')->load($prodID);

$categoryIds = $_product->getCategoryIds();

foreach($categoryIds as $categoryIds1) {

$_category = Mage::getModel('catalog/category')->load($categoryIds1);

//get all category ID in the current product.

echo $category_id = $_category->getId();

//get all the Category Name of the Current Product.

echo $category_name = $_category->getName();


If you just need to obtain the current category on a product page, the easiest way to do that is to get it from the Registry:

echo Mage::registry('current_category') ? Mage::registry('current_category')->getName() : 'No category';

To get an ID, use:

$catid = Mage::registry('current_category') ? Mage::registry('current_category')->getId() : null;
$_product = Mage::getModel('catalog/product')->load(prodId);
$ids = $_product->getCategoryId();
var_dump($ids); //this is category id

Below code is wrong:

$cat = Mage::getModel('catalog/product')->setId($ids);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.