1

Is it possible to get the category id from which the product is loaded in search result?

2

Yes it is possible but you have to keep in mind that one product can be assigned to several categories.

To retrieve the category Ids of a product you can use the following code:

$cats = $product->getCategoryIds();

Then if you want more details about the category you can loop through the array of IDs and retrieve data:

foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->load($category_id) ;
    echo $_cat->getName();
} 
6
  • I just need the cat id from which the product is loaded when I choose to view the product displayed in search result. Btw will this work for PDP? – Swetha Oct 1 '15 at 9:44
  • The problem is: search result does not load products from a category. It loads the products directly from the database based on your search criteria. – Raphael at Digital Pianism Oct 1 '15 at 10:10
  • Thats what my problem is.. :( – Swetha Oct 1 '15 at 10:12
  • Is there any way to get cat info in search page????? – Swetha Oct 1 '15 at 10:13
  • Yes, as I said in my answer, that's the only way to do it. – Raphael at Digital Pianism Oct 1 '15 at 10:15
0

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

$cats = $product->getCategoryIds();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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