TransWikia.com

Where are Commerce 2 hooks (or Events) in Commerce 2

Drupal Answers Asked by Wally Kolcz on October 26, 2021

I am working on replacing our payment system from D7/DC1 to D8/DC2 and the original code makes use of hooks. I have replaced some of them with events but I cannot find any information on the DC2 versions of:

mymodule_form_commerce_checkout_form_checkout_alter()
mymodule_form_commerce_checkout_form_review_alter()
mymodule_form_commerce_checkout_complete()
mymodule_form_commerce_cart_add_to_cart_form_alter()

Are they still the same or much like the former cart (add, update, remove) hooks, these are also events but I can’t locate documentation on them. Anyone know the equivalent events or where to find the docs on these (or their replacements)

/* Small Update */

I found OrderEvents and I think I need ORDER_PAID to replace mymodule_form_commerce_checkout_complete()

Does this look correct?

I added OrderEvents::ORDER_PAID => ‘orderPaid’ to my getSubscribedEvents(){} array and have:

public function orderPaid(OrderEvent $event){
    $order = $event->getEntity();
    //Some internal logic here
    $order->save();
  }

//Updated
Tried a hook form alter and ran through my checkout process and nothing is showing in the logs

function srs_hook_form_alter(&$form, $form_state, $form_id){
  Drupal::logger('hook_form_alter fires')->info('<pre><code>' . print_r($form_id, TRUE) . '</code></pre>');

}

One Answer

as you've discovered, you can look in any module's src/Event/*Events.php file to find a list of their events. Using the order paid event is one way to react once you know that payment has been collected, but note that in scenarios where an order is placed with only a payment authorization (and not a capture), that event would not have been fired yet.

That may be fine in your case - if not, I'd recommend the commerce_order.place.pre_transition or commerce_order.place.post_transition events that are triggered when the order is placed. That event is coming from the state machine module and correlates to order workflow transitions. You can read more about it in the module's readme.

As for form alters, those exist in D8 as they did in D7.

Answered by Ryan Szrama on October 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP