7

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')
        ->addAttributeToSelect('*')     
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount()
        ->addCategoryFilter($cat)
        ->setPageSize($productCount); 

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

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

5 Answers 5

11

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);
}
4

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();
}
5
  • 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
    Mar 29, 2016 at 7:12
  • @Kaka It will work if you visit directly product page.I have checked it
    – Arunendra
    Mar 29, 2016 at 7:20
  • please refer updated question.
    – Ajay Patel
    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
    Mar 29, 2016 at 7:57
  • @Arunendra could you pls help me with my own post magento.stackexchange.com/q/284557/57334
    – zus
    Aug 6, 2019 at 12:37
4

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();

}
2

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;
1
$_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);
1

Your Answer

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

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