How to get category name and id on product page on Magento

On product page, generally its location is app/design/frontend/{designPackageName}/{themeName}/template/catalog/product/view, put the following code on addtocart.phtml file:

<?php
$_helper = $this->helper('catalog/output');
	$_category_detail=Mage::registry('current_category');
	echo  $_category_detail->getName(); //gives current  category name
	echo $_category_detail->getId(); //gives current category id
?>

Comments

comments

1 Comment

  1. Hello,
    Very nice script..i used it in my project on product page for showing random related products of current category without selection from admin panel..

    $_helper = $this->helper(‘catalog/output’);
    $_category_detail=Mage::registry(‘current_category’);
    //echo $_category_detail->getName(); //gives current category name
    //echo $_category_detail->getId(); //gives current category id
    $products = $_category_detail->getProductCollection()->joinField(‘inventory_in_stock’, ‘cataloginventory_stock_item’, ‘is_in_stock’, ‘product_id=entity_id’,’is_in_stock>=0′, ‘left’)->setOrder(‘inventory_in_stock’,’asc’); ;
    $products->getSelect()->order(new Zend_Db_Expr(‘RAND()’));

    $i=1;
    foreach($products as $product){ ?>
    if($i > 4) break; ?>
    $_item = Mage::getModel(‘catalog/product’)->load($product[‘entity_id’]);

    }

Leave a Reply