如何为每个新订单,产品等创建DrupalCommerce 2X事件订阅
每当在DrupalCommerce 2X中创建新的订单产品时,我需要能够编写一个获取订单,产品等的插件.但我似乎无法弄清楚Commerce如何要求我做到这一点.我没有看到可以提供数据的* events文件.
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.
看起来Commerce需要我创建一个单独的Event Flow插件来添加我想要的步骤,但是我似乎找不到有关实现自己的Event Flow的文档.
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.
在创建订单或产品时,您可以引导我正确执行代码吗?我在正确的道路上吗?您可以指向"Events/EventSubscriber 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?
完成订单后,系统调用commerce_order.place.post_transition.因此您需要在结帐完成时创建一个事件.
On order complete, system call the commerce_order.place.post_transition. so You need to create an Event on Checkout complete.
应对转变
示例-对顺序位置"转换做出反应.
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.
}
}
向Drupal介绍您的事件订阅者
Telling Drupal About Your Event Subscriber
您的事件订阅者应添加到模块基本目录中的{module} .services.yml中.
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' }
有关更多参考,请查看以下URL: https://docs. drupalcommerce.org/commerce2/developer-guide/orders/react-to-workflow-transitions#reacting-to-transitions
For more reference review the following URL: https://docs.drupalcommerce.org/commerce2/developer-guide/orders/react-to-workflow-transitions#reacting-to-transitions