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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aveeva
    New Member
    • Apr 2019
    • 33

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

    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



    Code:
     
    <?xml version="1.0"?>
    <config>
        <modules>
            <Brst_Test>
                <active>true</active>
                <codePool>community</codePool>
            </Brst_Test>
        </modules>
    </config>

    This is my observer file

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

    Code:
    <?php
    ini_set('display_errors', '1');
    
    // Mage::log('Hy observer called', null, 'logfile.log');
    class Brst_Test_Model_Observer
    {
        //Put any event as per your requirement
        public function logCartAdd() {
            $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");
            }
    
            // $quote = Mage::getSingleton('checkout/session')->getQuote();
            // if ($quote->hasProductId(2)) 
            //{
            //  Mage::getSingleton("core/session")->addError("Cart has Special Product you can not add another");
            //  return;
            // }
            $quote = Mage::getModel('checkout/cart')->getQuote();
            foreach ($quote->getAllItems() as $item) {
                $productId = $item->getProductId();
                if($productId==31588){
                    Mage::throwException("Cart has Special Product you can not add another");
                }
            }
    
        }
    }
    ?>
    app/code/community/Brst/Test/etc/config.xml

    Code:
    <?xml version="1.0"?>
    <config>
        <modules>
            <Brst_Test>
               <version>0.1.0</version>
            </Brst_Test>
        </modules>
        <global>
            <models>
                <brst_test>
                    <class>Brst_Test_Model</class>
                </brst_test>
            </models>
        </global>
        <frontend>
            <events>
                <controller_action_predispatch_checkout_cart_add>
                    <observers>
                        <brst_test_log_cart_add>
                            <class>brst_test/observer</class>
                            <method>logCartAdd</method>
                        </brst_test_log_cart_add>
                    </observers>
                </controller_action_predispatch_checkout_cart_add>
            </events>
        </frontend>
    </config>
    Not working, how to solve the error?
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    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.

    Comment

    • aveeva
      New Member
      • Apr 2019
      • 33

      #3
      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?

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        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.

        Comment

        Working...