1

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

1 Answer 1

1

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

<events>
    <sales_quote_merge_before>
        <observers>
            <cart_validation>
                <class>MyModule_CartValidation_Model_Observer</class>
                <method>quoteMergeBefore</method>
            </cart_update>
        </observers>
    </sales_quote_merge_before>

</events>

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 items in cart
        *  
        */
        $isRestricted = $this - > hasRestrictedItems($quoteItem, $product);
        if ($isRestricted) {
            Mage::getSingleton("core/session") - > addError("You can not add this product");
            return;
        }

    }
}

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

1

Your Answer

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

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