I implemented a custom service which listens to the add to cart event and redirects to checkout. However I still see the add to cart message. We need a way to disable that. And possibly making redirect to checkout easier.
The event subscriber needs
public static function getSubscribedEvents() {
$events = [
CartEvents::CART_ENTITY_ADD => ['onProductAdded', 1000],
KernelEvents::RESPONSE => ['checkRedirectIssued', -10]
];
return $events;
}
And then.
public function onProductAdded(CartEntityAddEvent $event) {
\Drupal::requestStack()->getCurrentRequest()->attributes->set('_checkout_redirect_url', Url::fromRoute('commerce_checkout.form', [
'commerce_order' => $event->getCart()->id(),
])->toString());
}
/**
* Checks if a redirect rules action was executed.
*
* Redirects to the provided url if there is one.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The response event.
*/
public function checkRedirectIssued(FilterResponseEvent $event) {
$request = $event->getRequest();
$redirect_url = $request->attributes->get('_checkout_redirect_url');
if (isset($redirect_url)) {
$event->setResponse(new RedirectResponse($redirect_url));
}
}
Comments
Comment #2
scotthooker commentedRelated to https://www.drupal.org/node/2810723 ?
Comment #3
scotthooker commentedIs there any sense in the "Add to cart" label being a setting on the variation or store somewhere other than it is now?
Comment #4
jtolj commentedWe needed to do something similar to implement a minimum quantity check, which if not met removes the added product from the cart. I moved the drupal_set_message() from the submitForm() handler to an event subscriber, which can then be stopped using $event->stopPropagation();
I'm not sure this is the best approach to allow preventing this message from appearing. In our case, we're removing the product from the cart, so stopping event propagation has no side effects. In your case, you would want other events to still fire.
Regardless the drupal_set_message() definitely doesn't belong where it is currently.
Patch attached...
Comment #5
mglamanjtolj thanks, this looks like right first step.
Comment #6
jtolj commentedUpdated patch... I'd read some bad documentation on setting the priority of an event subscriber and also fixed reference to the event name to use the provided constant (CartEvents::CART_ENTITY_ADD).
Comment #7
mglamanWe do not need to inject the translation service, using the trait is fine enough.Works for me. This is a pattern in core and prevents the trait from accessing \Drupal as a fallback.
Do we really need to set such a low priority? I think omitting the priority is fine.
End users can remove or adjust using a CompilerPass in their code if needed.
Comment #8
jtolj commentedUpdated patch to remove hard-coded priority value.
Comment #9
mglamanThumbs up from me. jtolij can you put this patch as a pull request on https://github.com/drupalcommerce/commerce? If not, I can later today. We don't run our tests on DrupalCI (yet), only via GitHub + Travis
Comment #10
jtolj commentedSure thing...
https://github.com/drupalcommerce/commerce/pull/670
Thanks!
Comment #11
jeroent+1 For this change.
This change is AFAIS necessary for modules like Commerce add to cart confirmation
Comment #12
jeroentTried the patch and it was working as expected. Code also looks good so RTBC!
Comment #13
jantoine commentedReroll
Comment #14
drugan commentedAlso, the Add to cart message might be disabled like this:
https://gist.github.com/drugan/ae65acc2e552454fba14a2067c254a54#file-dru...
Comment #15
imyaro commented@drugan please forget about the d7-style alters
Just override CartEventSubscriber class with your own one to allow disable message.
But as for me - it will be great to manage this via configuration
Comment #16
jeroentI added a way to override the event subscriber on the PR#670.
Comment #17
heddnLet's add the dsm in an event subscriber. that way you can much more easily disable it. Working on that now.
Comment #18
heddnActually, I should look at the patch first. It does exactly what I was proposing/planning to do. RTBC.
Comment #19
heddnFor those not familiar with event subscribers, you can easily disable other events using the approach listed in https://symfony.com/doc/current/components/event_dispatcher.html#stoppin...
If you register your custom event before the one in this patch, you can simply disable propagation and effectively disable or change the dsm. Actually, it sounds like a great contrib module, build a method to override and/or disable the message via this cool new commerce_cart_message module!
Comment #20
joshmillerA lot of work was made in a similar project, if the maintainers would see fit to give a little credit nod to sorab.v6 for his work there, that would be very classy.
#2905624: Need a simple alter hook for the add to cart message
Comment #21
bojanz commentedThe event subscriber is injecting a cartProvider that is never used.
Comment #22
bojanz commentedComment #23
bojanz commentedUpdating credits.
Comment #25
bojanz commentedComment #27
bojanz commentedImproved and committed. Thanks, everyone.