cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Categories of products In Observer?

Re: How to get Categories of products In Observer?

Hello @Aveeva 

 

There are many cases but can not write full code, I can suggest how it can work or if any issue in native magento functionality I will help. Hope you will understand. Thanks

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

Re: How to get Categories of products In Observer?

Okay in the observer, how to load selected category using id's and selected website?

Re: How to get Categories of products In Observer?

$product = $observer->getProduct();$category_id = $product->getCategoryIds();$category_products  = Mage::getModel('catalog/category')
			->setWebsiteId(2)		 // load website id
			->load($category_id); 		 // load category 

from above your code, how can i add id inside the if-loop, 

 

eg:

 

   $product = Mage::getModel('catalog/product')
                        ->load(Mage::app()->getRequest()->getParam('product', 0));
        $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

        if ($product->getId()==31588 && $cart_qty > 0) {
            Mage::throwException("You can not add This special Product, empty cart before add it");
        }

FYI -> here 31588 is product id.

Re: How to get Categories of products In Observer?

Hello @Aveeva 

 

Try as follows:

 

foreach($category_products as $categoryprodcut){
  if($prodcut->getId() == $categoryprodcut->getId() && $cart_qty > 0){
    throw error
  }
}

$categoryprodcut->getId() pr $categoryprodcut->getEntiryId()
Manish Mittal
https://www.manishmittal.com/

Re: How to get Categories of products In Observer?

Initially, check with cart, if other products have cart restricted category of products not eligible to add to cart, show message like If you want Kolu Padi, Purchase alone not mixed with other Products, so by initial we need to give category id then only loop check, here how can i add id in the line of -> 

if($prodcut->getId() == $categoryprodcut->getId() && $cart_qty > 0)

Here where i can give my id?

Re: How to get Categories of products In Observer?

Hello @Aveeva 

Why want to place loop here, category loop will come before that check my code which I shared already with you.

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

Re: How to get Categories of products In Observer?

Let me explain,

Here is restricted based on products id :

 

 $product = Mage::getModel('catalog/product')
                        ->load(Mage::app()->getRequest()->getParam('product', 0));
        $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

        if ($product->getId()==31588 && $cart_qty > 0) {
            Mage::throwException("You can not add This special Product, empty cart before add it");
        }

From above code given product id : 31588, if we don't give id, then how observer check with cart product category id, we need to give right? 

Re: How to get Categories of products In Observer?

I just update my observer,

 

<?php
class Gta_KolupadiRestrict_Model_Observer {

	public function cartevent(Varien_Event_Observer $observer) {

		// Load product
		$product = $observer->getProduct();
		$prodID = $product->getId();
		$_product = Mage::getModel('catalog/product')->load($prodID);

		// get category id
		$categoryIds = $_product->getCategoryIds();

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

		// check conditiion cart has other products 
		if(in_array(681, $categoryIds) && $cart_qty > 0) {
			Mage::throwException("You can not add This special Product, empty cart before add it");
		}
	
		// check if other products add to cart
		$quote = Mage::getModel('checkout/cart')->getQuote();
		foreach ($quote->getAllItems() as $item) {
			$_product1 = $item->getProduct();
	
			$categoryIds1 = $_product1->getCategoryIds();
			if(in_array(681, $categoryIds1)) {
				Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");
				break;
			}
		}
	}   
}
?>

error :

 

a:5:{i:0;s:67:"If you want Kolu Padi, Purchase alone not mixed with other Products";i:1;s:1835:"#0 /home/abc/public_html/app/code/local/Gta/KolupadiRestrict/Model/Observer.php(21): Mage::throwException('If you want Kol...')
#1 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(1358): Gta_KolupadiRestrict_Model_Observer->cartevent(Object(Varien_Event_Observer))
#2 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(1337): Mage_Core_Model_App->_callObserverMethod(Object(Gta_KolupadiRestrict_Model_Observer), 'cartevent', Object(Varien_Event_Observer))
#3 /home/abc/public_html/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('checkout_cart_p...', Array)
#4 /home/abc/public_html/app/code/core/Mage/Checkout/Model/Cart.php(290): Mage::dispatchEvent('checkout_cart_p...', Array)
#5 /home/abc/public_html/app/code/local/Cmsmart/AjaxCart/controllers/IndexController.php(315): Mage_Checkout_Model_Cart->addProduct('5071', Array)
#6 /home/abc/public_html/app/code/local/Cmsmart/AjaxCart/controllers/IndexController.php(133): Cmsmart_AjaxCart_IndexController->tryaddAction(Object(Mage_Catalog_Model_Product), Array)
#7 /home/abc/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Cmsmart_AjaxCart_IndexController->indexAction()
#8 /home/abc/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#9 /home/abc/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#10 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#11 /home/abc/public_html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#12 /home/abc/public_html/australia/index.php(93): Mage::run('australia', 'website')
#13 {main}";s:3:"url";s:100:"/ajaxcart/index/index/?form_key=MtlJbvKkqPQKCLJR&product=5071&related_product=&qty=1&_=1565155804563";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:9:"australia";}

Re: How to get Categories of products In Observer?

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>Gta_KolupadiRestrict_Model_Observer</class>
                        <method>cartevent</method>
                    </Gta_KolupadiRestrict_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>      
        </events>
    </global>  
</config>