By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Mageto 1.9 - Category of Products with Observer condition Error?

aveeva
P: 28
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

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <config>
  3.     <modules>
  4.         <Gta_KolupadiRestrict>
  5.             <active>true</active>
  6.             <codepool>local</codepool>
  7.         </Gta_KolupadiRestrict>
  8.     </modules>
  9. </config>
  10.  
app/code/local/Gta/KolupadiRestrict/etc/config.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <Gta_KolupadiRestrict>
  5.             <version>1.0.0</version>
  6.         </Gta_KolupadiRestrict>
  7.     </modules>
  8.     <global> 
  9.         <models>
  10.             <gta_kolupadirestrict>
  11.                 <class>Gta_KolupadiRestrict_Model</class>
  12.             </gta_kolupadirestrict>
  13.         </models>
  14.         <events>
  15.             <checkout_cart_product_add_after>
  16.                 <observers>
  17.                     <Gta_KolupadiRestrict_Model_Observer>   
  18.                         <type>singleton</type>
  19.                         <class>Kolupadi_Restrict_Model_Observer</class>
  20.                         <method>cartevent</method>
  21.                     </Gta_KolupadiRestrict_Model_Observer>
  22.                 </observers>
  23.             </checkout_cart_product_add_after>      
  24.         </events>
  25.     </global>   
  26. </config>
  27.  
app/code/local/Gta/KolupadiRestrict/Model/Observer.php

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     Mage::log('fine dude', null, 'logfile.log');
  3.     //create class
  4.     class Gta_KolupadiRestrict_Model_Observer
  5.     {
  6.  
  7.         //create function
  8.         public function cartevent(Varien_Event_Observer $observer)
  9.         {
  10.             $category_id = array(680, 894, 895) ; //category ids
  11.  
  12.             $category_products  = Mage::getModel('catalog/category')
  13.                                         ->setWebsiteId(2);           // load website id
  14.                                         ->load($category_id);        // load category 
  15.  
  16.             // check cart qty status                            
  17.             $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemQty();  
  18.  
  19.             //logic
  20.             if($category_products && $cart_qty > 0  )
  21.             {
  22.                 Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");    
  23.             }
  24.  
  25.              //check if cart have products 
  26.              $quote = Mage::getModel('checkout/cart')->getQuote();
  27.              foreach($quote->getAllItems() as $item)
  28.              {
  29.                 $productId  = $item->getCategoryId();
  30.  
  31.                 if($productId == $category_id)
  32.                 {
  33.                     Mage::throwException("Cart has Special Product you can not add another");
  34.                 }
  35.              }      
  36.         }       
  37.     }
  38. ?>
  39.  
Observer not trigger. Does anyone help me?


I am not asking code help, i just want to know if my observer calling correct?
2 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.