How to Add PHP script in add-to-cart function like if other products are trying to add add-to-cart, supposed selected id based products[restricted products] already available in add-to-cart here restrict and show message like not allowed to add-to-cart, please purchase alone, if already some other products available in cart condition like you need to purchase this product alone.

Using Magento add-to-cart button Function how to achieve my above condition?


1 Answer 1


Create an event observer for event sales_quote_merge_before. Create a custom module and put this in config.xml.



Create Observer.php in [MyModule]/[CartValidation]/Model and put this code.

class MyModule_CartValidation_Model_Observer {
    public function quoteMergeBefore($observer) {
        // Get Existing Cart Items
        $quoteItem = Mage::getSingleton('checkout/cart') - > getQuote() - > getItemsCollection();

        //Get current product trying to be added
        $product = Mage::getModel('catalog/product')
            - > load(Mage::app() - > getRequest() - > getParam('product', 0));

        *  Your code logic to see restricted
        $isRestricted = $this - > hasRestrictedItems($quoteItem, $product);
        if ($isRestricted) {
            Mage::getSingleton("core/session") - > addError("You can not add this product");


Make sure you implement your restricted products logic in hasRestrictedItems().


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.