2

I am on the product page. I have a bundle product and when I press the add to cart button I want to add a simple product programmatically to cart. I'm using this event: checkout_cart_product_add_after for my observer.

This is my code:

    public function cartProductUpdateAfter($observer)
    {
        $this->cartProductAddAfter($observer);
    }
    public function cartProductAddAfter($observer)
    {
        $productToAdd = Mage::getModel('catalog/product');
        $productToAdd->load(1745);

        $this->_addProductToCart($productToAdd, 1);
    }
    protected function _addProductToCart($product, $qty)
    {
        $cart = Mage::getSingleton('checkout/cart');
        if ($product->getId()) {
            // infinite loop probably
            $cart->addProduct($product, $qty);
            return true;
        }
        return false;
    }

I'm getting a maxium execution time exceeded error message, problably because it calls itself. Can you give me some ideas how to fix that ?

1 Answer 1

1

Replace last function with this:

   protected function _addProductToCart($product, $qty)
    {
        $cart = Mage::getSingleton('checkout/session')->getQuote();
        if ($product->getId()) {
            // infinite loop probably
            $cart->addProduct($product, $qty);
            $cart->save();
            return true;
        }
        return false;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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