1

I have two different types of products in my shop.

One type is for subscriptions - the other one for single deliveries

It should not be possible to buy products of this two types at the same time in the same cart. At best if one type is in cart you shouldnt be able to add a product with the other type to the cart.

Any suggestions would be much appreciated.

1 Answer 1

1

Take a look at Stop add to cart and supply message to user in Magento

<controller_action_predispatch_checkout_cart_add>
        <observers>
            <aquait_aquait_name>
                <type>singleton</type>
                <class>Aquait_Aquait_Model_Observer</class>
                <method>checkfunction</method>
            </aquait_aquait_name>
        </observers>
    </controller_action_predispatch_checkout_cart_add>


public function checkfunction($observer) {

    if($observer->getEvent()->getControllerAction()->getFullActionName() == "checkout_cart_add")
    {
        $productId = Mage::app()->getRequest()->getParam('product');
        $product = Mage::getModel('catalog/product')->load($productId);

        $found = false;
        $cartHelper = Mage::helper('checkout/cart');
        $items = $cartHelper->getCart()->getItems();
        foreach ($items as $item) {
           //logic for item that you can not buy together
           if ($item->getProduct()->getSku() == 'xxx') {
                $found = true;          
                break;
            }
        }

        if($found) {
            Mage::getSingleton('core/session')->addNotice('You can not buy is item conbination');

            /* not the best redirect but don`t know how to */
            header("Location: " . $product->getProductUrl());
            die();

        }
    }

}
1
  • Exactly what I was looking for. Awesome man! Complete solution will be posted later. Feb 24, 2016 at 12:57

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.