How to Subscribe to DrupalCommerce 2X Events for every new Order, Product etc is created

别等时光非礼了梦想. 提交于 2019-12-11 14:24:03

问题


I need to be able to write a Plugin that gets the orders, product, etc., whenever a new Order, Product is created in DrupalCommerce 2X. but I can't seem to figure out how Commerce wants me to do it. I don't see any *events files that would give me the data.

It looks like Commerce wants me to create a separate Event Flow plugin that would add the step I want, but I can't seem to find documentation about implementing my own Event Flow.

Can you guide me to the right path of running my code when the order or product is created? Am I on the right path? Can you point to Events/EventSubscriber Flow development docs?


回答1:


On order complete, system call the commerce_order.place.post_transition. so You need to create an Event on Checkout complete.

Reacting to Transitions

Example - reacting to the order 'place' transition.

// mymodule/src/EventSubscriber/MyModuleEventSubscriber.php
namespace Drupal\my_module\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;

class MyModuleEventSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    // The format for adding a state machine event to subscribe to is:
    // {group}.{transition key}.pre_transition or {group}.{transition key}.post_transition
    // depending on when you want to react.
    $events = ['commerce_order.place.post_transition' => 'onOrderPlace'];
    return $events;
  }

  public function onOrderPlace(WorkflowTransitionEvent $event) {
    // @todo Write code that will run when the subscribed event fires.
  }
}

Telling Drupal About Your Event Subscriber

Your event subscriber should be added to {module}.services.yml in the base directory of your module.

The following would register the event subscriber in the previous section:

# mymodule.services.yml
services:
  my_module_event_subscriber:
    class: '\Drupal\my_module\EventSubscriber\MyModuleEventSubscriber'
    tags:
      - { name: 'event_subscriber' }

For more reference review the following URL: https://docs.drupalcommerce.org/commerce2/developer-guide/orders/react-to-workflow-transitions#reacting-to-transitions



来源:https://stackoverflow.com/questions/50830689/how-to-subscribe-to-drupalcommerce-2x-events-for-every-new-order-product-etc-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!