Saurabh
Saurabh

Reputation: 148

How to display category name on cart page in Magento

I am trying to display the product category on cart page but it returns empty array. The file which i use is /app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php

My code is

public function getCustumcatId(){
    $proid=$this->getProduct()->getId();
    $mproduct = Mage::getModel('catalog/product')->load($proid);
    $ids=$this->getProduct()->getCategoryIds();
    return $ids;
}

Upvotes: 1

Views: 5593

Answers (2)

Saurabh
Saurabh

Reputation: 148

After number of trial and errors finally i have answer of question. And I want to share it.. I hope it helps someone..

Copy /app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php to /app/code/local/Mage/Checkout/Block/Cart/Item/Renderer.php and make this change:

public function getCustumcatId()
{
    $proid=$this->getProduct()->getId();
    $categoryIds=$this->getProduct()->getCategoryIds($proid);
    foreach($categoryIds as $categoryId)
    {
        $category = Mage::getModel('catalog/category')->load($categoryId);
    }
    return  $category->getName();
}

And for display category name on Cart page open /app/design/frontend/em0014/default/template/checkout/cart/item/default.php near line no.36

<?php 
    $mycat_name=$this->htmlEscape($this->getCustumcatId());
    echo "Categoryid".$mycat_name."<br>";
?>

Upvotes: 0

Magento Guy
Magento Guy

Reputation: 2493

A Product can be assigned to many categories, so without significant modification you may not be able to find out which category that product came from, however this might be able to do what you're after from that class:

<?php

$_catCollection = $this->getItem()->getProduct()->getCategoryCollection();

foreach ($_catCollection as $_category) {
  // do stuff with your Mage_Catalog_Model_Category
}

Upvotes: 3

Related Questions