2

I have a checkbox in product view page and if it is checked and user clicks on "Add to Cart" I want to add additional product to the cart.

How to do this with an observer? Which event I need to watch?

EDIT: This seems to work

public function cartProductAddAfter()
{
    $product = Mage::getModel('catalog/product');
    $productId = $product->getIdBySku('X6020R-652');
    $qty = 1;
    $productToAdd = $product->load($productId);
    $cart = Mage::getSingleton('checkout/session')->getQuote();
    $cart->addProduct($productToAdd, $qty);
    $cart->save();
    Mage::getSingleton('core/session')->addSuccess('Product added to cart successfully.');
}
3

This is what you are looking for (unable to mark it as duplicate): Original answer by MageWorx

Declare event observers in config.xml:

<events>
    <checkout_cart_product_add_after>
        <observers>
            <custommodule>
                <class>custommodule/observer</class>
                <method>cartProductAddAfter</method>
            </custommodule>
        </observers>
    </checkout_cart_product_add_after>
    <checkout_cart_product_update_after>
        <observers>
            <custommodule>
                <class>custommodule/observer</class>
                <method>cartProductUpdateAfter</method>
            </custommodule>
        </observers>
    </checkout_cart_product_update_after>
</events>

Develop the Observers handlers:

class Vendor_Custommodule_Model_Observer 
{
    /* If you'd like to do the same while updating the shopping cart*/
    public function cartProductUpdateAfter($observer)
    {
        $this->cartProductAddAfter($observer);
    }

    public function cartProductAddAfter($observer)
    {
        $product = $observer->getEvent()->getProduct();
        $currentItem = $observer->getEvent()->getQuoteItem();
        $quote = $currentItem->getQuote();
        $quoteItems = $quote->getItems();

        /* Detect Product ID and Qty programmatically */
        $idToAdd = "ANY PRODUCT ID";
        $qty = 1;

        $productToAdd = Mage::getModel('catalog/product');
        /* @var $productToAdd Mage_Catalog_Model_Product */
        $productToAdd->load($idToAdd);

        $this->_addProductToCart($productToAdd, $qty);
    }

    protected function _addProductToCart($product, $qty)
    {
        $cart = Mage::getSingleton('checkout/cart');
        /* @var $cart Mage_Checkout_Model_Cart */
        if ($product->getId()) {
            $cart->addProduct($product, $qty);
            return true;
        }
        return false;
    }
}
1

This worked.

public function cartProductAddAfter()
{
    $product = Mage::getModel('catalog/product');
    $productId = $product->getIdBySku('X6020R-652');
    $qty = 1;
    $productToAdd = $product->load($productId);
    $cart = Mage::getSingleton('checkout/session')->getQuote();
    $cart->addProduct($productToAdd, $qty);
    $cart->save();
    Mage::getSingleton('core/session')->addSuccess('Product added to cart successfully.');
}

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.