Magento – Get Category name from Products when adding to Shopping Cart

magento-1.8PHP

Using Magento 1.8v.

In my customization when customer adding products to cart.
Some products qty should not be changeable (i.e qty is fixed) and some other products qty available to change .

Example :

cat1 
  products 
cat2
  subcat
    products
  subcat
    products
cat3
  subcat
     products

For Example:
cat1 products allowed for qty change so when adding to shopping cart it will be 1 and remaining category should not allowed so it is stock item value .

Im trying below code in this class Mage/Checkout/Model/Cart.php but throwing error

$categoryIds=$this->getProduct()->getCategoryIds($productid);
    foreach($categoryIds as $categoryId)    
    $category = Mage::getModel('catalog/category')->load($categoryId);
    if ($category->getName() == 'cat1')   
        $minimumQty = $product->getStockItem()->getMinSaleQty();  
   else if($category->getName() == 'cat2')                        
         minimumQty=stock_item->loadByProduct($product)->getQty();   

How do i get category name ?

Best Answer

Try this.

// Get category id by product id.
    $quote = Mage::getModel('checkout/cart')->getQuote();
    $items = $quote->getAllVisibleItems();
    $count = 0;
    foreach ($items as $item) {
        $product = $item->getProduct();
        if ($count < 10) {
            $categories = $product->getCategoryCollection()
                        ->addAttributeToSelect('name')
                        ->addAttributeToFilter('is_active', array('eq' => 1));
            foreach($categories as $cat) {
                if(trim($cat) == 'xxx') { // If category_name == 'xxx' 
                    // Do something.
                    break;
                }
            }
            $count++;
        } else {
            break;
        }
    }