1

I am developing custom module for 'Buy one get one free (BOGO)' functionality.
I am done with custom form and grid at admin side for setting mapping of product-X and product-Y for BOGO.
I have written observer for checkout_cart_product_add_after event to check respective product-Y for currently added product and also successfully getting respective mapped product.
But I am stuck at adding product-Y in cart programmatically. I have tried below code:

$prodY = Mage::getModel('catalog/product')->load(<ID_OF_MAPPED_PROD-Y>);
if($prodY) {
    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $cart->addProduct($prodY, array('product_id' => $prodY->getId(), 'qty' => 1));
    $cart->save();
}

My web store is crashing here and I am unable to find any error.
What wrong I am doing here? or Which will be most suitable event to handle w.r.t my scenario of adding product progrmmatically in cart?
Can anyone please help me to get out of this situation?
Thanks in advance !!!

Find related code below:

config.xml

<frontend>
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <bogo_item_add>
                    <class> [MyName_MyModule]_Model_Observer</class>
                    <method>addRespectiveBogoItem</method>
                </bogo_item_add>
            </observers>
        </checkout_cart_product_add_after>
    </events>
</frontend>


Observer function part of code

$prodY = Mage::getModel('catalog/product')->load(<ID_OF_MAPPED_PROD-Y>);
        Mage::getSingleton('checkout/session')->addSuccess($prodY->getName() . ' will get added to cart with currently added product');
        if($prodY) {
            Mage::helper('[HELPER]')->addBogoProductToCart($prodY);
        }

Helper function

public function addBogoProductToCart($product) {
    try {
        $cart = Mage::getModel('checkout/cart');
        $cart->init();
        $cart->addProduct($product, array( 'product_id' => $product->getId(), 'qty' => 1));
        $cart->save();
    } catch(Exception $e) {
        Mage::getSingleton('checkout/session')->addError($e->getMessage());
    }
}



Error stack trace
enter image description here

0

To allow question to close, but is the same as found here:

Adding a product to cart programatically

The Fatal error: Maximum function nesting level of '200' reached, aborting! is caused by the fact that you are running into an endless loop.

Your action of $cart->save() is initiating your own routine, which is then action again, saving the cart, and then action your routine again etc....

To block this, you can check the cart items first, and determine if the item being injected is already in cart, and then simply exit the routine if so.

  • Your hint helped me.. event handler was getting called recursively.. so it was going into loop and hitting the limit... I done this by setting a flag in cart session... for setting session value I referred magento.stackexchange.com/questions/15543/… .. – Chaitanya Apr 27 '16 at 10:12
0

Add a try catch exception to find what is going wrong. So replace your code with below code

    $prodY = Mage::getModel('catalog/product')->load(<ID_OF_MAPPED_PROD-Y>);
    if($prodY) {
        $cart = Mage::getModel('checkout/cart');
        $cart->init();
        $cart->addProduct($prodY, array('product_id' => $prodY->getId(), 'qty' => 1));
    try{
        $cart->save();}
    catch(Exception $ex){
    die($ex->getMessage());
    }
}

Hope this will help you to get your problem.

  • when error reporting is turned on.. I am getting following erro.... Fatal error: Maximum function nesting level of '100' reached, aborting! – Chaitanya Apr 27 '16 at 7:18
  • You can not call $cart = Mage::getModel('checkout/cart');` on a cart event. So try to add a function to helper or block or in model and call that function here. – Asish Hira Apr 27 '16 at 7:21
  • tried your suggestion.. added a function in helper which add product in cart.. but no success.. still stuck at same error... :( – Chaitanya Apr 27 '16 at 7:30
  • show all codes you added – Asish Hira Apr 27 '16 at 7:34
  • I have added related codes from source code as well as error stack trace... – Chaitanya Apr 27 '16 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.