0

I have a custom event in my commerce 2 shop.

filename: custom_module/src/Event/OrderShippedEvent.php

namespace Drupal\custom_module\Event;

class OrderShippedEvent extends EventBase
{
  public const EVENT_NAME = 'custom_events.order_shipped';

  /**
   * @var \Drupal\commerce_order\Entity\OrderInterface
   */
  protected OrderInterface $order;

  protected RequestStack $requestStack;

  public function __construct(OrderInterface $order)
  {
    $this->order = $order;
    $this->requestStack = \Drupal::service('request_stack');
  }

And a eventsubscriber

filename: custom_module/src/EventSubscriber/OrderShippedSubscriber.php

class OrderShippedSubscriber implements EventSubscriberInterface
{

  /**
   * The log storage.
   *
   * @var \Drupal\commerce_log\LogStorageInterface
   */
  protected $logStorage;

  public function __construct(
    protected RequestStack $stack,
    protected EntityTypeManagerInterface $entityTypeManager)
  {
    $this->logStorage = $entityTypeManager->getStorage('commerce_log');
  }

  public static function getSubscribedEvents()
  {
    $events[OrderShippedEvent::EVENT_NAME][] = ['onOrderShipped', 100];

    return $events;
  }

  public function onOrderShipped(OrderShippedEvent $event): void
  {
    $order = $event->getOrder();

    // Do some magic to the order 
  }

My modules services.yml file contains this

  custom_events.order_shipped:
    class: Drupal\custom_module\EventSubscriber\OrderShippedSubscriber
    arguments: [ '@request_stack', '@entity_type.manager' ]
    tags:
      - { name: 'event_subscriber' }

And more on that, i'm using the https://www.drupal.org/project/commerce_email module, to send mail when this event is fired.

filename: custom_module/src/Plugin/Commerce/EmailEvent/OrderShippedEvent.php

/**
 * @CommerceEmailEvent(
 *    id = "order_shipped_notification",
 *    label = @Translation("Order Shipped"),
 *    event_name = "custom_events.order_shipped",
 *    entity_type = "commerce_order",
 *   priority = "-100"
 *  )
 */
class OrderShippedEvent extends EmailEventBase
{

  public function extractEntityFromEvent(Event $event)
  {
    return $event->getOrder();
  }

}

From a custom AdvancedQueueJob i try to trigger the event so the magic is happening to the order, and an email is sent.

Only "magic happens" no mail was sent.

If i use the exact code from a .module file both is fired

filename: another_module.module


function another_module_page_attachments(array &$attachments) {
  // hardcoded to test a single order.
  $order = \Drupal\commerce_order\Entity\Order::load(54);
  $event = new OrderShippedEvent($order);
  /** @var \Symfony\Component\EventDispatcher\EventDispatcher $event_dispatcher */
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, OrderShippedEvent::EVENT_NAME);
}

The codesnippet i use to test is

  // hardcoded to test a single order.
  $order = \Drupal\commerce_order\Entity\Order::load(54);
  $event = new OrderShippedEvent($order);
  /** @var \Symfony\Component\EventDispatcher\EventDispatcher $event_dispatcher */
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, OrderShippedEvent::EVENT_NAME);

And from the Advancedqueuejob only the magic to the order event is fired.

I have tried almost everything, hope you guys can help me out here.

3
  • More to the question: when used from module i'm authenticated, and from a advancedqueuejob im running from Drush ? Feb 9 at 12:56
  • And if fired from cron UI it works, but not from drush advancedqueue:process queuename Feb 9 at 13:02
  • It's because KernelEvent:REQUEST isn't fired from drush Feb 10 at 8:56

0

Your Answer

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

Browse other questions tagged or ask your own question.