0

Its a uncommon issue that i face while getting the product category in product detail page.

My code is working fine in staging and not working in live.

I used below code to get the current category.

if (Mage::registry('current_category'))
{
  // Category Name
  $current_category =  Mage::registry('current_category')->getName();
  // Category ID
  $category_id =  Mage::registry('current_category')->getId();
  // Catergory Url
  $url = Mage::getModel("catalog/category")->load($category_id)->getUrl();
}

This code is working fine in staging not in live.

I tried this approach also(Method 2)

$categoryIds = $_product->getCategoryIds();

    foreach($categoryIds as $categoryId) {
          $category = Mage::getModel('catalog/category')->load($categoryId);
          $categorynm =  $category->getName();
          $url = $category->getUrl();    
    }

Using this method also i am unable to get the category url in live. Please help me to find this issue.its totally uncommon scenario works in staging and not working in live.

I tried several method from stack exchange questions but i am unable to fix this issue.

1

Take a look at how to get category name of current product (on product detail page) in magento

The reason it may work in dev is because you are going directly from a category to a product page. If you visit the product page directly nothing will be display because a product can be attached to multiple categories so magento does not now which category should get display.

The current_category registry is not always set, see

viewAction() in app/code/core/Mage/Catalog/controllers/ProductController.php

prepareAndRender() in app/code/core/Mage/Catalog/Helper/Product/View.php

initProduct() in app/code/core/Mage/Catalog/Helper/Product.php Line #321

To get the category id on a product detail page you need to do something like

$categoryIds = $_product->getCategoryIds();

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

    echo $_category->getName();
 }
5
  • I am using the same file, same coding and the hosts are different. what can i look with the updated section? – Mujahidh Nov 18 '15 at 16:57
  • its not working for me – Mujahidh Nov 18 '15 at 17:13
  • no erros, i am just echo the category name in a hidden field.its not view – Mujahidh Nov 18 '15 at 17:15
  • 1
    Try debugging to make sure the is assigned to a category. Check to see what each of these value return $categoryIds, $firstCategoryId ... etc – Renon Stewart Nov 18 '15 at 17:17
  • Thank you R.S.the product not assign to any category. – Mujahidh Nov 18 '15 at 17:34

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.