如何在Shopware6中通过代码触发Flow Builder流程?
使用FlowExecutor触发Flow流程实现需求
核心思路
你需要在订单打标签完成后,主动触发指定的Flow流程(原流程为配送状态变更时触发)。Shopware的FlowExecutor是手动触发Flow的核心服务,核心步骤为:注入服务、准备触发所需的上下文与数据、调用执行方法触发流程。
具体实现步骤
1. 注入FlowExecutor服务
在你的命令类或服务类的构造函数中,注入FlowExecutorInterface:
use Shopware\Core\Framework\Event\Flow\FlowExecutorInterface; use Shopware\Core\Content\Order\OrderRepositoryInterface; use Symfony\Component\Console\Command\Command; class YourOrderTaggingCommand extends Command { private FlowExecutorInterface $flowExecutor; private OrderRepositoryInterface $orderRepository; public function __construct( FlowExecutorInterface $flowExecutor, OrderRepositoryInterface $orderRepository ) { $this->flowExecutor = $flowExecutor; $this->orderRepository = $orderRepository; parent::__construct(); } }
2. 扩展打标签方法,添加Flow触发逻辑
打完标签后,根据你的Flow触发条件,选择对应的触发方式:
方式一:复用标签添加事件触发
如果你的Flow已配置为监听OrderTagAddedEvent(标签添加时触发),可以手动构造该事件并触发流程:
use Shopware\Core\Content\Flow\Dispatching\StorableFlow; use Shopware\Core\Framework\Context; use Shopware\Core\Content\Order\Event\OrderTagAddedEvent; use Shopware\Core\Content\Tag\TagRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; // 需在构造函数中注入TagRepositoryInterface private TagRepositoryInterface $tagRepository; private function tagOrderAndTriggerFlow(Context $context, string $orderId, string $tagId): void { // 执行打标签操作 $this->orderRepository->update([ [ 'id' => $orderId, 'tags' => [['id' => $tagId]] ] ], $context); // 加载订单与标签数据 $order = $this->orderRepository->search(new Criteria([$orderId]), $context)->first(); $tag = $this->tagRepository->search(new Criteria([$tagId]), $context)->first(); if (!$order || !$tag) { return; } // 构造标签添加事件 $event = new OrderTagAddedEvent($order, $tag, $context); // 触发Flow流程 $this->flowExecutor->run( new StorableFlow( $event->getName(), $context, [ 'order' => $order, 'tag' => $tag ], $event ) ); }
方式二:直接触发指定Flow(已知Flow ID)
如果你明确知道目标Flow的ID,可以直接指定Flow ID触发,无需匹配事件:
use Shopware\Core\Content\Flow\Dispatching\StorableFlow; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; private function tagOrderAndTriggerFlow(Context $context, string $orderId, string $tagId, string $targetFlowId): void { // 执行打标签操作 $this->orderRepository->update([ [ 'id' => $orderId, 'tags' => [['id' => $tagId]] ] ], $context); // 加载订单数据 $order = $this->orderRepository->search(new Criteria([$orderId]), $context)->first(); if (!$order) { return; } // 构造Flow触发实例,指定目标Flow ID $storableFlow = new StorableFlow( 'custom.order.tag.trigger', // 自定义事件名,可随意填写 $context, ['order' => $order], null, [$targetFlowId] // 传入要触发的Flow ID数组 ); // 执行Flow $this->flowExecutor->run($storableFlow); }
3. 关键注意事项
- 事件匹配:如果使用事件触发方式,需确保Flow的触发事件与你构造的事件名称完全一致,否则流程不会启动。
- 上下文完整性:必须传递正确的
Context,保证流程中的权限、语言、店铺等信息与操作场景匹配。 - 数据加载:构造事件或Flow实例时,需确保订单、标签等关联数据已完整加载,避免流程执行时出现数据缺失。
- 重复触发规避:若原流程已在配送状态变更时触发,需在Flow中添加额外条件(如标签存在且未发送过邮件),或在代码中判断订单是否已触发过该流程,避免重复发送邮件。
内容的提问来源于stack exchange,提问作者stack stack
相关产品推荐
相关产品推荐

