1

I am trying to create a dynamic event subscription, for drupal8 commerce order state.

My use case is the following. I want to create an invoice for any order which goes into state e.g. PENDING. There is a custom state/transition yml which i have created.

Which state transition should trigger an invoice creation, i want to make this configurable.

So, the admin should be able to select specific states, in a custom settings form, and the system should only create invoices when an order enters to one of these states.

I need to create an event subscriber, which is dynamic according to the settings that the admin has entered.

In terms of code, in the event subscriber, i have the following code

public static function getSubscribedEvents() {
  $events = ['commerce_order.place.post_transition' =>'onTriggerInvoiceCreation'];
  return $events;
}

public function onTriggerInvoiceCreation(WorkflowTransitionEvent $event) {
  $invoice_creation_states = explode(",", $this->config_factory->get("my_custom_module.settings")->get("invoice_creation_states"));
  $current_state = $event->getToState()->getId();

  if(in_array($current_state, $invoice_creation_states)) {
    //create the invoice
  }
}

So, a far as i understand, i need to do one of the following

In function getSubscribedEvents, i read the my_module.settings from configuration and find a way to construct the correct event subscriber. So, instead of

$events = ['commerce_order.place.post_transition' =>'onTriggerInvoiceCreation'];

i should do something like the following

$invoice_creation_states = explode(",", $this->config_factory->get("my_custom_module.settings")->get("invoice_creation_states"));
for($i=0;$i<count($invoice_creation_states);$i++{
  $events = ['commerce_order.'.$invoice_creation_states[$i].'.post_transition' =>'onTriggerInvoiceCreation'];

As this function is static, i don't seem to be able to get hold of the config service in order to get the $invoice_creation_states.

So next thought is to create a more generic event listener, for all order state changes, and in the corresponding listener callback e.g. onTriggerInvoiceCreation i could get the custom settings and make the above condition in order to execute or not the invoice creation code.

Question: Is there a more generic transition event than commerce_order.TRANSITION_ID.TRANSITION_PHASE

something like

commerce_order.ANY_TRANSITION.ANY_PHASE ?

1

This has recently been implemented in Fire generic events when transition are applied [#2832415].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.