As @markoshust blogged in 2012 (!), there is no checkout_cart_product_add_before event in Magento!
Even though there is a checkout_cart_product_add_after event. Now we're stuck with controller_action_predispatch_checkout_cart_add which of course does not catch all cases since there are multiple (custom) ways to add a product to the cart.
Best option now is to create a plugin, but an event would be nicer.
checkout_cart_product_add_before and checkout_cart_product_add_after are dispatched.checkout_cart_product_add_after event is dispatched.Hi @peterjaap. Thank you for your report.
To help us process this issue please make sure that you provided the following information:
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
@magento-engcom-team give me {$VERSION} instance
where {$VERSION} is version tags (starting from 2.2.0+) or develop branches (2.2-develop +).
For more details, please, review the Magento Contributor Assistant documentation.
@peterjaap do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
@peterjaap, thank you for your report.
We've acknowledged the issue and added to our backlog.
Hi @peterjaap
Thank for your report. Could you please provide use cases when plugins should not be used but event if preferable
Best option now is to create a plugin, but an event would be nicer.
It is not big deal to add the event, but we need to have a strong reason for that
Thank you
IMO with events you can hook into at a more specific place.
Example to work around missing _before event in magento 1. You'll have to remove the item in _after event instead of prevent adding befor.
$quote = $observer->getQuote();
$product = $observer->getProduct();
if ($something) {
foreach ($quote->getAllItems() as $item) {
if ($something == $item->getProduct()->getData('abc')) {
$quote->removeItem($item->getId());
Mage::throwException(
Mage::helper('sales')->__('You cannot add that product to your cart.')
);
}
}
}
Where is the advantage of using a plugin?
I'm curious to hear from @okorshenko as well why a Plugin should be preferred over an event. The event pattern is in my opinion a much more clear way to do this and should always be preferred over a plugin.
@sreichel , @peterjaap , why before or around plugin will not solve your task?
@peterjaap , why event pattern is more clear? Plugins have defined interface, so you're always sure what arguments to expect. In the same time, in observers you get any data from the event, which is too abstract to provide you information about available data specific for your event.
In this specific use case, just for consistency we can add the event. But in general, it's better to use plugins as they provide more guarantees for backward compatibility.
Hi @sreichel , described behavior can be reached by usage of before or around plugin for \Magento\Checkout\Model\Cart::addProduct method. You may provide any validation and throw an exception if the product does not match the conditions.
Also, we prefer plugins for data modification instead of observers, you can find the corresponding section in our Technical Guidelines.
Hi @peterjaap I hope you got the answers. Anyway we will accept this pull request https://github.com/magento/magento2/pull/18080 to resolve your concern
Thank you
@sidolov I understand that for data modificaton purposes a Plugin is preferred, but in cases where the data is not modified, I would say that an event is preferred.
Hi @peterjaap. Thank you for your report.
The issue has been fixed in magento/magento2#18080 by @leandro-rosa in 2.3-develop branch
Related commit(s):
The fix will be available with the upcoming 2.3.1 release.
@peterjaap just noticed this, thanks for raising this issue. definitely needed in certain circumstances or just to give the developer the flexibility to choose the implementation they wish to use.
looks like all merged in, so going to close this one out.
Most helpful comment
Hi @peterjaap I hope you got the answers. Anyway we will accept this pull request https://github.com/magento/magento2/pull/18080 to resolve your concern
Thank you