Writing Event Subscriber for Order Complete event in Drupal 8 Commerce 2.x

Commerce guys has done a great job in building Commerce 2.x module for Drupal 8.

Let us see how to write an Event subscriber for successful order completion event, Simply said “Execute your custom code when an order is successfully generated by user”.

So for beginners, what are Events and Event subscribers?

According to Drupal documentation here, Events are defined as follows :

Events are part of the Symfony framework: they allow for different components of the system to interact and communicate with each other. Each event has a unique string name. One system component dispatches the event at an appropriate time; many events are dispatched by Drupal core and the Symfony framework in every request.

and Event subscribers are :

System components can register as event subscribers. when an event is dispatched, a method is called on each registered subscriber, allowing each one to react.

So Events are basically triggers and Event Subscribers are actions performed when their corresponding Events are triggered.

So we need to follow two simple steps for your code to execute on Order complete event.

  1. Tell Drupal that you want to listen to order complete event in MY_MODULE.services.yml (Assuming MY_MODULE is your module name).
MY_MODULE.order_complete:
class:
Drupal\MY_MODULE\EventSubscriber\OrderCompleteSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }

Replace MY_MODULE with your module name. The “arguments” above are useful to Dependency Inject any service you might want to use in your code in event subscriber and is optional. Any service/s can be used. More about Drupal 8 services here.

2. Create a directory structure as MY_MODULE/src/EventSubscriber and put your “OrderCompleteSubscriber” class inside. So your class now lives at below path:

MY_MODULE/src/EventSubscriber/OrderCompleteSubscriber.php

Now your “OrderCompleteSubscriber” should implement “EventSubscriberInterface”.

<?php

namespace
Drupal\MY_MODULE\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Drupal\Core\Entity\EntityTypeManager;

/**
* Class OrderCompleteSubscriber.
*
*
@package Drupal\MY_MODULE
*/
class OrderCompleteSubscriber implements EventSubscriberInterface {

/**
* Drupal\Core\Entity\EntityTypeManager definition.
*
*
@var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;

/**
* Constructor.
*/
public function __construct(EntityTypeManager $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}

/**
* {
@inheritdoc}
*/
static function getSubscribedEvents() {
$events['commerce_order.place.post_transition'] = ['orderCompleteHandler'];

return $events;
}

/**
* This method is called whenever the commerce_order.place.post_transition event is
* dispatched.
*
*
@param WorkflowTransitionEvent $event
*/
public function orderCompleteHandler(WorkflowTransitionEvent $event) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $event->getEntity();
    // Order items in the cart.
$items = $order->getItems();

// Write your custom logic here.
  }

}
$events[‘commerce_order.place.post_transition’] = [‘orderCompleteHandler’];

The above line in method “getSubscribedEvents” tells Drupal, the event we are listening to, in our case “commerce_order.place.post_transition”. Something special about this event is that, instead of directly using Event Dispatcher service to dispatch an event in Commerce module, this is dispatched by “State Machine module when a workflow transition is made. This event is dispatched whenever an order is successfully placed.

orderCompleteHandler” method is invoked whenever the above event is dispatched.

For best Developer Experience, use Drupal console for creating the above.

Drupal Console Command:

drupal ges

-Sumanth Reddy