You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware >=6.5插件中如何执行自定义Flow触发器?

Shopware 6.5 自定义Flow触发器事件无响应的解决步骤

核心问题

你的事件未被Shopware Flow系统识别,导致Flow监听器未绑定到该事件,进而出现hasListeners返回空、事件进入orphanedEvents的情况。以下是遗漏的关键步骤:


1. 给事件类添加Flow事件注解

Shopware 6.5使用#[FlowEvent]注解标记可被Flow Builder识别的事件,替代了旧版的BusinessEventInterface。修改你的事件类:

use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Context;
use Shopware\Core\Checkout\Order\OrderEntity;
use Symfony\Contracts\EventDispatcher\Event;

// 注解必须添加,指定事件唯一标识、显示名称和描述
#[FlowEvent(
    name: 'order.sent',
    label: '订单已发送',
    description: '当订单完成发送操作时触发'
)]
class OrderSentEvent extends Event implements OrderAware
{
    private string $orderId;
    private Context $context;

    public function __construct(OrderEntity $order, Context $context)
    {
        $this->orderId = $order->getId();
        $this->context = $context;
    }

    // 必须实现OrderAware接口的方法,返回订单ID
    public function getOrderId(): string
    {
        return $this->orderId;
    }

    // 可选:提供事件名称的静态方法,方便触发时使用
    public static function getName(): string
    {
        return 'order.sent';
    }

    // 可选:提供上下文 getter,若Flow动作需要使用上下文数据
    public function getContext(): Context
    {
        return $this->context;
    }
}

2. 确保事件类被注册到服务容器

插件中的事件类需要被Shopware容器识别,可通过两种方式实现:

  • 自动配置:在插件的src/Resources/config/services.xml中开启自动扫描:
<services>
    <defaults autowire="true" autoconfigure="true" public="false" />
    <!-- 扫描插件src目录下的所有类,自动注册为服务 -->
    <prototype namespace="YourPluginNamespace\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests}" />
</services>
  • 手动配置:若未开启自动扫描,手动在services.xml中注册事件类并添加Flow标签:
<service id="YourPluginNamespace\Event\OrderSentEvent">
    <tag name="shopware.flow.event" />
</service>

3. 正确触发事件

触发事件时,必须使用#[FlowEvent]注解中指定的事件名称,否则Flow监听器无法匹配:

// 方式1:手动指定事件名称
$this->eventDispatcher->dispatch(new OrderSentEvent($order, $context), 'order.sent');

// 方式2:使用事件类的静态getName方法(需先实现该方法)
$this->eventDispatcher->dispatch(new OrderSentEvent($order, $context), OrderSentEvent::getName());

4. 验证Flow配置与缓存

  • 进入Shopware后台的Flow Builder,确认已创建包含你自定义触发器(订单已发送)的Flow,且Flow处于激活状态。
  • 执行缓存清理命令,确保注解和配置生效:
bin/console cache:clear

内容的提问来源于stack exchange,提问作者gaxweb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 08:05:28