0

So, I have an event subscriber for Drupal commerce that listens for "commerce_order.place.post_transition" and will send a bespoke order email (due to not being able to change some of the things like subject title in the commerce module).

Commerce also has an event subscriber listening to "commerce_order.place.post_transition" which will send an order receipt email. I need to unsubscribe this event to prevent sending duplicate emails but searching through google and comments sections of articles has lead me to a dead end.

Any ideas?

4 Answers 4

3

Remove the event subcriber service from the container:

my_module/src/MyModuleServiceProvider.php

namespace Drupal\my_module;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;

/**
 * Remove the foo event subscriber.
 */
class MyModuleServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
    $container->removeDefinition('foo_event_subscriber'); 
  }

}

You find the service name in the MODULE.services.yml file of the other module.

More info https://www.drupal.org/docs/8/api/services-and-dependency-injection/altering-existing-services-providing-dynamic-services

1
  • Cheers, this worked a treat.
    – Oliver
    Feb 8, 2019 at 15:27
1

This doesn't answer your question specifically but is an alternative. If it's the standard commerce email you could go to "Commerce > configuration > Orders > Order types" and within each order type you can control to "Email the customer a receipt when an order is placed"

2
  • Yeah, my current workaround is to untick that, which prevents commerce from sending emails and makes our bespoke event subscriber the only one to send emails. Unfortunately it is easy for any member of the admin to team to tick that box again and start sending duplicate emails.
    – Oliver
    Feb 8, 2019 at 11:19
  • You might find it easier to form_alter and prevent that option being ticked rather than disabling the subscriber
    – Leigh
    Feb 8, 2019 at 11:25
1

Decorate the existing event as described in this article: https://matthieuscarset.com/en/blog/how-safely-override-or-extend-other-services-methods-drupal-8

In mymodule/src/EventSubscriber/MySubscriber.php:

namespace Drupal\mymodule\EventSubscriber;

class MySubscriber extends ParentSubscriber {

  /**
   * {@inheritDoc}
   */
  public function decoratedMethod(...$args) {
    parent::decoratedMethod(...$args); // or whatever you want to do
  }
}

In mymodule/mymodule.services.yml:

services:
  mymodule.myservice:
    public: false
    class: Drupal\mymodule\EventSubscriber\mySubscriber
    decorates: decorated_provider.decorated_service_name
-2

and welcome to Drupal Answers.

Assuming you're using Drupal 8, you may try the below code to hide the email fieldset:

function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if ($form_id == "commerce_order_type_edit_form") {
        $form["emails"] = [
            "#checked" => FALSE,
        ];
    }
}

Hope that helps.

1
  • The question is about events, not form alters.
    – Jonathan
    Nov 1, 2020 at 15:10

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.