I am trying to add a link on my website's product pages and redirect the users based on the category that the product belongs to. For example, I have a category chairs which is having a Category Id of 39 and I am having another category desks for which the category id is 52.

Now my issue with the above scenario is when I am moving into the product page through category pages I am getting the above-mentioned category Id's properly and the URL will be


but when I am trying to search for the product directly from the search box and move into the product page from there the URL becomes


the category id becomes 2 and this happens with every category and it shows the category name as default category when I go to the product page through search box. How can I resolve this? kindly let me know if there is any parameter that doesn't change on the product page which can enable me to display the content based on the category the product belongs to.

  • @sv3n Hi, I created an array and using foreach I filled the array with all the categories and checked using in_array if the category name existed in the array I did whatever I wanted to do. Sorry for the late reply and thank you for your reply to me. Jul 11, 2017 at 12:04
  • So this proplem is solved? If you you can also add your solution here and accept you own answer to close this. :)
    – sv3n
    Jul 12, 2017 at 13:49
  • Can you try my solution too? Think it should perform better and if the result is correct please leave an upvote :) (Btw ... 2nd line isn't required since $this->getProduct() returns complete loaded product data)
    – sv3n
    Jul 16, 2017 at 23:12
  • @sv3n Will try once again. Jul 17, 2017 at 11:22

2 Answers 2


This is because in Magento a product can belong to multiple categories. When you're navigating through a category to a product detail page, Magento registers the category page as the "active category", therefore you can obtain the category details for that specific category on the product page.

When you're navigation directly to a product page, through a search result page or direct URL for example, Magento doesn't known which category you came from because it is not registered yet.

You can obtain the categories the product belongs to using this:

$categories = $_product->getCategoryIds();
foreach($categories as $categoryId){
    $_category = Mage::getModel('catalog/category')->load($categoryId);
    $categoryData = $_category->getData();
    // Do something

So I solved it by creating an array in which I filled all the categories that the product is a part of. And then I searched for the category that I wanted and carried out whatever I wanted to do if the condition was satisfied.

$productId = $this->getProduct()->getId(); // YOUR PRODUCT ID
$product = Mage::getModel('catalog/product')->load($productId);

$categoryIds = $product->getCategoryIds();

$categories = Mage::getModel('catalog/category')
                    ->addAttributeToFilter('entity_id', $categoryIds);

foreach ($categories as $category) {
    $catstr[] = $category->getName();

After the above code, I searched for the category inside the array $catstr and it solved my issue.


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.