1

Hope you're all doing well.

I need some guidance in how or if some tool already exist for this:

If an specific product enters the shopping cart, automatically one other specific product appears in the cart, but with discount.

I would really appreciate some help.

0

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>

Then create observer file in your module :

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);
        //put code apply code discount here
    }

    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;
    }
}

Then put your code for apply discount after line

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

Hope it helps !

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.