cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Categories of products In Observer?

How to get Categories of products In Observer?

I need to add a condition for my category in the observer, how can I call category?

18 REPLIES 18

Re: How to get Categories of products In Observer?

Hello @Aveeva 

 

Try below shared code:

 

$product = $observer->getProduct();
$catIds = $product->getCategoryIds();
//var_dump($catIds);

 

Manish Mittal
https://www.manishmittal.com/

Re: How to get Categories of products In Observer?

My workout :

 

$category_id = array(680, 894, 895) ; //category ids
			
	$category_products  = Mage::getModel('catalog/category')
			->setWebsiteId(2)		 // load website id
			->load($category_id); 		 // load category 
			

How can i update with my above code from your code?

Re: How to get Categories of products In Observer?

Hello @Aveeva 

 

Try like:

 

$product = $observer->getProduct();
$category_id = $product->getCategoryIds();
$category_products  = Mage::getModel('catalog/category')
			->setWebsiteId(2)		 // load website id
			->load($category_id); 		 // load category 
			
Manish Mittal
https://www.manishmittal.com/

Re: How to get Categories of products In Observer?

For me Make my self-clarity, here where i can give my id's?

Re: How to get Categories of products In Observer?

I need to give condition like,

 

Condition: I have restricted category of products [category id = 680, 894, 895], this category of products should be purchase alone, not mixed with other products.

Workout: Case 1: If cart had other products, if the customer tries to add restricted category products trigger observer like not eligible to add to cart and display a message like If you want this product, Purchase alone not mixed with other Products

 

case 2: If cart had a restricted category of products if customer try to add non-restricted products trigger observer like not eligible to add to cart and display a message like Cart has Special Product you can not add another

 

How can i set my restricted category of id's to check with the cart?

 

Re: How to get Categories of products In Observer?

Hello @Aveeva 

 

If you have some products and want to purchase alone then you can create a configuration with special products skus or ids. then you are going to add product to cart can check already any product id present in that configuration or not and based on that you can put condition.

 

Suppose in configuration comma separated id's 2,45,43

then you have to check current product id presents in configuration or already any products in cart based on that you can put conditions

Manish Mittal
https://www.manishmittal.com/

Re: How to get Categories of products In Observer?

Yes,

 

### start the Function   ####

public function cartevent(Varien_Event_Observer $observer)
        {
            #### load category id's & website id  ###     
            $category_products = Mage::getModel('catalog/category')
                                ->addAttributeToFilter('category_id', array('in' => array('680','894','895')))
                                ->setWebsiteId(2);           // load website id      

            ###   check cart  ###                        
            $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemQty();
            
            ### condition ###
            if($category_products && $cart_qty > 0 )
            {
                Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");    
            }

             
             $quote = Mage::getModel('checkout/cart')->getQuote();

            foreach($quote->getAllItems() as $item)
            {
                $productId  = $item->getCategoryId();
                
                if($productId == $category_products)
                {
                    Mage::throwException("Cart has Special Product you can not add another");
                }
            }   

        }

 

 

 

 

Still my observer does not trigger, if i wrong in my code, pls correct me.

Re: How to get Categories of products In Observer?

My whole observer : 

<?php 
	// Mage::log('fine dude', null, 'logfile.log');
	//create class
	class Gta_KolupadiRestrict_Model_Observer
	{
		
		//create function
		public function cartevent(Varien_Event_Observer $observer)
		{
			// $category_id = array(680, 894, 895) ; //category ids
			
			$category_products  = Mage::getModel('catalog/category')
								->addAttributeToFilter('category_id', array('in' => array('680','894','895')))
								->setWebsiteId(2);			    // load website id		

			// check cart qty status							
			$cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemQty();  
			
			//logic
			if($category_products && $cart_qty > 0  )
			{
				Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");	
			}
			
			 //check if cart have products 
			 $quote = Mage::getModel('checkout/cart')->getQuote();

			foreach($quote->getAllItems() as $item)
			{
				$productId	= $item->getCategoryId();
				
				if($productId == $category_products)
				{
					Mage::throwException("Cart has Special Product you can not add another");
				}
			}	 

		}		
	}
?>

 

 

 

Re: How to get Categories of products In Observer?

Condition:

I have restricted category of products [category id = 680, 894, 895], this category of products should be purchase alone, not mixed with other products.

 

Workout:

Case 1: If cart had other products, if the customer tries to add restricted category products trigger observer like not eligible to add to cart and display a message like If you want this product, Purchase alone not mixed with other Products

 

case 2: If cart had a restricted category of products if customer try to add non-restricted products trigger observer like not eligible to add to cart and display a message like Cart has Special Product you can not add another

code :

 

app/etc/modules/Gta_KolupadiRestrict.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Gta_KolupadiRestrict>
            <active>true</active>
            <codepool>local</codepool>
        </Gta_KolupadiRestrict>
    </modules>
</config>

app/code/local/Gta/KolupadiRestrict/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Gta_KolupadiRestrict>
            <version>1.0.0</version>
        </Gta_KolupadiRestrict>
    </modules>
    <global> 
        <models>
            <gta_kolupadirestrict>
                <class>Gta_KolupadiRestrict_Model</class>
            </gta_kolupadirestrict>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Gta_KolupadiRestrict_Model_Observer>   
                        <type>singleton</type>
                        <class>Kolupadi_Restrict_Model_Observer</class>
                        <method>cartevent</method>
                    </Gta_KolupadiRestrict_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>      
        </events>
    </global>   
</config>

app/code/local/Gta/KolupadiRestrict/Model/Observer.php

 

<?php 
    // Mage::log('fine dude', null, 'logfile.log');
    //create class
    class Gta_KolupadiRestrict_Model_Observer
    {

        //create function
        public function cartevent(Varien_Event_Observer $observer)
        {
            // $category_id = array(680, 894, 895) ; //category ids

            $category_products  = Mage::getModel('catalog/category')
                                ->addAttributeToFilter('category_id', array('in' => array('680','894','895')))
                                ->setWebsiteId(2);              // load website id      

            // check cart qty status                            
            $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemQty();  

            //logic
            if($category_products && $cart_qty > 0  )
            {
                Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");    
            }

             //check if cart have products 
             $quote = Mage::getModel('checkout/cart')->getQuote();

            foreach($quote->getAllItems() as $item)
            {
                $productId  = $item->getCategoryId();

                if($productId == $category_products)
                {
                    Mage::throwException("Cart has Special Product you can not add another");
                }
            }    
        }       
    }
?>

Here is my complete module.