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?
-
magento.stackexchange.com/questions/279094/…Ketan Borada– Ketan Borada2019年06月24日 09:40:54 +00:00Commented Jun 24, 2019 at 9:40
1 Answer 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().
-
Join with chat.stackexchange.com/rooms/95367/…zus– zus2019年06月25日 05:27:38 +00:00Commented Jun 25, 2019 at 5:27