I want to get category name, which are products already added in shopping cart. But I want category name in shopping cart add action. Means /checkout/cart/add in addAction.

Thanks in addvanced.


Product do have more than one category. Do you assume, that your product is only added to one category? Then you can just:

$ids = $product->getCategoryIds()

better is to collect all category ids, then load a collection and take the categories from there.

Performance (maybe)

What else you can do is add a field to the quote_item, implement an observer and add the category name to the quote item. Then you can just $quoteItem->getCategoryName(). But I'm not sure whether you have the category while adding a product to the cart. If not, you need to get it somehow :-)

  • I have fetch already those product's category using this type of code. But I want those product's category name, which product is already in shopping cart. – Prashant Parekh Dec 11 '13 at 10:51
  • 1
    @PrashantParekh, this makes no sense. The product can be added to cart not through category (search, whishlist, reorder). – user487772 Dec 11 '13 at 10:53

$_item['product_id'] is a product id . We will find out the categoryid associated with the product.

$product = Mage::getModel('catalog/product')->load($_item['product_id']);
$cats = $product->getCategoryIds();

Now we have category ID(s). One product can associate with more then one category, this is Magento default nature.

 foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
        echo $_cat->getName();             

$_cat->getName(); is your product category name. This is beneficial if product is associate with a single category. If a product is associated with multiple category then we need category ID to get product category name.

  • 1
    How to get current product category id and cart products category id, if both are different trigger Observer, could you pls help me with my post magento.stackexchange.com/q/284557/57334 @Asheem Patro – zus Aug 8 '19 at 5:21

You can get category id by using product id at any where 1)first get product id and load catalog/product collection by this product id like

<?php  $product = Mage::getModel('catalog/product')->load($productid );?>

In that $productid is your product id variable ;

<?php  $product->getCategoryIds(); 
echo ($cats[0]); ?>


foreach($cats as $categoryId) {
    $category = Mage::getModel('catalog/category')->load($categoryId);
    echo $category->getName();  

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.