沃梦达 / 编程问答 / php问题 / 正文

如何为创建的每个新订单、产品等订阅 DrupalCommerce 2X 事件

How to Subscribe to DrupalCommerce 2X Events for every new Order, Product etc is created(如何为创建的每个新订单、产品等订阅 DrupalCommerce 2X 事件)

本文介绍了如何为创建的每个新订单、产品等订阅 DrupalCommerce 2X 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够编写一个插件来获取订单、产品等,每当在 DrupalCommerce 2X 中创建新订单、产品时.但我似乎无法弄清楚商务部希望我怎么做.我没有看到任何可以为我提供数据的 *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 希望我创建一个单独的事件流插件来添加我想要的步骤,但我似乎找不到关于实现我自己的事件流的文档.

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?

推荐答案

订单完成后,系统调用 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 Drupalmy_moduleEventSubscriber;

use SymfonyComponentEventDispatcherEventSubscriberInterface;
use Drupalstate_machineEventWorkflowTransitionEvent;

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: 'Drupalmy_moduleEventSubscriberMyModuleEventSubscriber'
    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

这篇关于如何为创建的每个新订单、产品等订阅 DrupalCommerce 2X 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何为创建的每个新订单、产品等订阅 DrupalCommerce 2X 事件

基础教程推荐