I need to be able to run code that does not output any data to the user after a user's checkout is complete, but I can't seem to figure out how Commerce wants me to do it. I don't see any *.api.php files that would give me payment hooks. It looks like Commerce wants me to create a separate Checkout Flow plugin that would add the step I want, but I can't seem to find documentation about implementing my own Checkout Flow.

Can you guide me to the right path of running my code when the checkout is complete? Am I on the right path? Can you point to Checkout Flow development docs?

Comments

kunalkursija’s picture

Hi @Hexorg,

When you say you want "to run code that does not output any data to the user after a user's checkout is complete", That means you want to ACT on an event (checkout complete).

In Drupal 7:
This was done by writing hooks or writing rules, Which Drupal executed at events.

In Drupal 8:

  • D8 has used Symfony's 'Event Dispatcher' Component. This encourages Extensibility.
  • In drupal commerce 2.x, Once the checkout completes - commerce module Dispatches the 'Checkout Complete' Event.
  • Now, If you want to write some code at this event, You will have to CATCH this event. (i.e - You need to subscribe to this event in your custom module by writing Event Subscriber)

FYR : Follow this link - https://www.chapterthree.com/blog/how-to-register-event-subscriber-drupal8

kunalkursija’s picture

Hi @hexorg,

You can write below function inside your service class:

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[CheckoutEvents::CHECKOUT_COMPLETE] = array('respondToCheckoutComplete', 0);
    return $events;
  }

In the above function,

  • "respondToCheckoutComplete" is another function present inside your service class. check below example.
  • "CheckoutEvents" is in another namespace so you this use Drupal\commerce_checkout\Event\CheckoutEvents; inside your service class file.
  public function respondToCheckoutComplete(CheckoutCompleteEvent $event) {
// WRITE BUSINESS LOGIC HERE !!!
}

Please note that "CheckoutCompleteEvent" is in another namespace, so you need to use it like below in your service class file:

use Drupal\commerce_checkout\Event\CheckoutCompleteEvent;

I Hope this helped you.

Hexorg’s picture

Yeah thanks! I actually ended up figuring this out by looking at the order module source code... is the change from hooks to the event interface documented anywhere? All of the documents I saw kept talking about hooks.

kunalkursija’s picture

Hi @Hexorg,

Even I am not sure as I too had to dig into commerce module to understand this and later on ended up learning about This Symfony component, Which is Great :)

sumanthkumarc’s picture

Actually, core has yet to move to event dispatch system. still uses hooks, and there's a issue for it. Commerce is one good contrib example which uses events hugely.

Parthvi’s picture

Hi @Kunal,

Thanks, I tried the above code in drupal 8 with Drupal commerce-8.x but it is not working.

In my custom modules service class, In function 'respondToCheckoutComplete' I added 'die' to test weather it is going there or not but it is not working like this.

Quick help is really Appreciated!

sumanthkumarc’s picture

Anyone coming looking for this, i have an article: https://medium.com/@sumanthkumarc/writing-event-subscriber-for-order-com...

codenerd33’s picture

You are a life saver. Thank you for putting this together!

3CWebDev’s picture

Thanks for the detailed write up.

Parthvi’s picture

Hey sumanthkumarc,

Thanks for the good tutorial.

I follow the steps you mentioned, but when customer place an order from website it didn't go to the function 'orderCompleteHandler'.

Can you please help!

I am using drupal 8.x and Drupal commerce-2.x.

Thank you in advance!

Alexandre360’s picture

Couldn't make it work. Can somebody upload a working example module for that ? Just something that write something to watchdog when an order is placed ?

Best regards,

Alex

SergeBr’s picture

In the very good exemple here :
https://www.chapterthree.com/blog/how-to-register-event-subscriber-drupal8

The MY_MODULE.services.yml file has to start with "services :" like this :

services:
    MY_MODULE.order_complete:
      class: Drupal\MY_MODULE\EventSubscriber\OrderCompleteSubscriber
      arguments: ['@entity_type.manager']
      tags:
        - { name: event_subscriber }

It was missing n the code.
and it works:-)

hoporr’s picture

This event is a general WorkflowState that we listen on.  As such, it could also be called if payment status is already "completed"  but reset to something else (like in the interface). Depending on your logic, you may need to run an update here as well.

You can check the fromState and toState like this in the event handler, and adapt your code accordingly in the handler:

 // states could be:  draft ....  completed
  $fromState = $event->getField()->getOriginalId();
  $toState   = $event->getTransition()->getToState()->getId();

Joe Huggans’s picture

I was wondering if anyone could point me in the right direction for hooking into the commerce checkout event in Drupal 7. I am wanting to grab a field value from the products and update a field in the user object with this. I also don't want to use rules for this

Joe Huggans’s picture

I figured it out 

function hook_commerce_order_update($order) {
  if ($order->status == 'completed') {
    
  }
}
mcdoolz’s picture

The brevity.  The simplicity.  Look at it people!  It's so small!  It just says what he did!  He moves on with life!  Agh, if I could touch you to shake your hand I would.

-
Be the occasion.

Joe Huggans’s picture

hahah thanks :D

hansrossel’s picture

This code will run every time an order that is in the "completed" state gets updated, so not only the first time when it reaches the "completed" state on checkout.

Joe Huggans’s picture

.

duit’s picture

For D8 wrote an article that relates. Dispatching Events with Entity CRUD hooks https://www.voodoo.works/blog/dispatching-events-entity-crud-hooks

jeetmail72’s picture

Hi

The above shared URL is not working. Can you please check and let me know.

https://www.voodoo.works/blog/dispatching-events-entity-crud-hooks

Joe Huggans’s picture

The shared URL? The link is broken also jeetmail72