Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Hexorg on
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
Event Subscribers !
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:
FYR : Follow this link - https://www.chapterthree.com/blog/how-to-register-event-subscriber-drupal8
Ex: Subscription
Hi @hexorg,
You can write below function inside your service class:
In the above function,
use Drupal\commerce_checkout\Event\CheckoutEvents;
inside your service class file.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.
Yeah thanks! I actually ended
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.
Not Sure about documentation
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 :)
Actually, core has yet to move to event dispatch system
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.
Not working for me with Drupal 8 with Drupal commerce-8.x
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!
Anyone coming looking for
Anyone coming looking for this, i have an article: https://medium.com/@sumanthkumarc/writing-event-subscriber-for-order-com...
Thank you!
You are a life saver. Thank you for putting this together!
Thanks!
Thanks for the detailed write up.
Custom code on Checkout complete
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!
Couldn't make it work. Can
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
In the very good exemple here
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:-)
check states
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();
I was wondering if anyone
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
I figured it out
I figured it out
I appreciate you.
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.
hahah thanks :D
hahah thanks :D
This code will run every time
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.
KOBA - Drupal Webdesign & Webdevelopment
Not sure why but the order
.
For D8 wrote an article that
For D8 wrote an article that relates. Dispatching Events with Entity CRUD hooks https://www.voodoo.works/blog/dispatching-events-entity-crud-hooks
URL not working
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
The shared URL? The link is
The shared URL? The link is broken also jeetmail72