468,121 Members | 1,509 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

Magento Own Custom Module for add-to-cart Not Working

aveeva
33
Created module like No other products add to cart if restricted product available in cart and vice versa.

My Module :

app/etc/modules/Brst_Test.xml



Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0"?>
  3. <config>
  4.     <modules>
  5.         <Brst_Test>
  6.             <active>true</active>
  7.             <codePool>community</codePool>
  8.         </Brst_Test>
  9.     </modules>
  10. </config>
  11.  

This is my observer file

app/code/community/Brst/Test/Model/Observer.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ini_set('display_errors', '1');
  3.  
  4. // Mage::log('Hy observer called', null, 'logfile.log');
  5. class Brst_Test_Model_Observer
  6. {
  7.     //Put any event as per your requirement
  8.     public function logCartAdd() {
  9.         $product = Mage::getModel('catalog/product')
  10.                         ->load(Mage::app()->getRequest()->getParam('product', 0));
  11.         $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
  12.  
  13.         if ($product->getId()==31588 && cart_qty > 0) {
  14.             Mage::throwException("You can not add This special Product, empty cart before add it");
  15.         }
  16.  
  17.         // $quote = Mage::getSingleton('checkout/session')->getQuote();
  18.         // if ($quote->hasProductId(2)) 
  19.         //{
  20.         //  Mage::getSingleton("core/session")->addError("Cart has Special Product you can not add another");
  21.         //  return;
  22.         // }
  23.         $quote = Mage::getModel('checkout/cart')->getQuote();
  24.         foreach ($quote->getAllItems() as $item) {
  25.             $productId = $item->getProductId();
  26.             if($productId==31588){
  27.                 Mage::throwException("Cart has Special Product you can not add another");
  28.             }
  29.         }
  30.  
  31.     }
  32. }
  33. ?>
  34.  
app/code/community/Brst/Test/etc/config.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <Brst_Test>
  5.            <version>0.1.0</version>
  6.         </Brst_Test>
  7.     </modules>
  8.     <global>
  9.         <models>
  10.             <brst_test>
  11.                 <class>Brst_Test_Model</class>
  12.             </brst_test>
  13.         </models>
  14.     </global>
  15.     <frontend>
  16.         <events>
  17.             <controller_action_predispatch_checkout_cart_add>
  18.                 <observers>
  19.                     <brst_test_log_cart_add>
  20.                         <class>brst_test/observer</class>
  21.                         <method>logCartAdd</method>
  22.                     </brst_test_log_cart_add>
  23.                 </observers>
  24.             </controller_action_predispatch_checkout_cart_add>
  25.         </events>
  26.     </frontend>
  27. </config>
  28.  
Not working, how to solve the error?
Jun 27 '19 #1
3 831
zmbd
5,400 Expert Mod 4TB
Not working, how to solve the error?
Why don't you invest a bit of time telling us exactly what it is that you are trying to do, what it is that you have tried to troubleshoot your own code, what it is that the code is doing when you attempt to execute it, and what errors are you receiving and when you are receiving those errors!

Posting a bunch of code and asking us to "fix it / Make it work for me" really isn't what we do here.
Jun 28 '19 #2
aveeva
33
I am new for Magento, I just created the module like if restricted products available in the cart no other products allow to adding to cart and vice versa, i am not asking full code, actually i give my complete code, i post because of atleast i am getting an idea for solving my issue. New people getting struggle initially developing module right?
Jun 28 '19 #3
zmbd
5,400 Expert Mod 4TB
Yes, Aveeva, new users struggle, I've been there myself and have received a great deal of help from a lot of places - especially here at Bytes.com :)

Even with that said, there are certain minimums that one needs to have in a question - I've stated those in my prior post.

If you had asked for code the thread would have been deleted.

As your post stands now, you've asked for someone to do the work for you - which isn't going to help you learn.

Please re-read my prior post and answer the questions - I think this thread is salvageable if you will do so or I would have closed and removed it.
Jun 28 '19 #4

Post your reply

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

Similar topics

1 post views Thread by Scott David Daniels | last post: by
15 posts views Thread by john fabiani | last post: by
6 posts views Thread by | last post: by
9 posts views Thread by Nick | last post: by
4 posts views Thread by Dmitry Duginov | last post: by
2 posts views Thread by xenophon | last post: by
1 post views Thread by Elie Medeiros via .NET 247 | last post: by
18 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.