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?

Asad Khan
1,48612 silver badges38 bronze badges
asked Jun 24, 2019 at 8:49
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().

answered Jun 24, 2019 at 10:29
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.