Shopware6:订单支付后订阅哪个活动?

我想知道如果我想在客户创建订单后付款时触发某个功能,应该使用哪个事件。


我已经尝试过这个:state_enter.order_transaction.state.paid => 'onOrderCheckout'。


不幸的是,它给出了一个错误:“ 警告:使用未定义的常量 state_enter - 假设为 'state_enter'(这将在 PHP 的未来版本中引发错误)*”。


这是我的订阅者:



namespace Emakers\TransmissionPlugin\Subscriber;


use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;

use Symfony\Component\HttpKernel\Event\ExceptionEvent;

use Shopware\Core\Checkout\Order\OrderEntity;

use Shopware\Core\Checkout\Order\OrderEvents;

use Shopware\Core\System\StateMachine\Event;

use Shopware\Core\Framework\Event\EventData\EntityType;

use Shopware\Core\System\SystemConfig\SystemConfigService;

use Symfony\Component\HttpKernel\KernelEvents;

use Symfony\Component\DependencyInjection\ContainerInterface;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

use Emakers\TransmissionPlugin\Services\ShopwareConnectService;



class OrderSubscriber implements EventSubscriberInterface

{

    /**

     * @ContainerInterface $container

     */

    private $container;



     /**

     * @var datetime

     */

     private $now;



     public function __construct(ContainerInterface $container) {

                $this->container = $container;

                $this->now = date('Y-m-d H:i:s');

     }


    public static function getSubscribedEvents(): array

    {

        return [

                'state_enter.order_transaction.state.paid'   => 'onOrderCheckout',

        ];

    }

        public function orOrderCheckout($event)

        {

                var_dump($event);

                die('here we are');

        }

}


梵蒂冈之花
浏览 88回答 1
1回答

海绵宝宝撒

我为此使用 StateMachineTransitionEvent。使用 Shopware 6.2 进行测试。<?php declare(strict_types=1);namespace Your\Namespace;use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;use Shopware\Core\Checkout\Order\OrderEntity;use Shopware\Core\Framework\Context;use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class OrderStateSubscriber implements EventSubscriberInterface {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @var EntityRepositoryInterface&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $stateMachineStateRepository;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @var EntityRepositoryInterface&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $orderRepository;&nbsp; &nbsp; public function __construct(EntityRepositoryInterface $stateMachineStateRepository, EntityRepositoryInterface $orderRepository)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->stateMachineStateRepository = $stateMachineStateRepository;&nbsp; &nbsp; &nbsp; &nbsp; $this->orderRepository = $orderRepository;&nbsp; &nbsp; }&nbsp; &nbsp; public static function getSubscribedEvents(): array&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StateMachineTransitionEvent::class => 'onStateTransition'&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }&nbsp; &nbsp; public function onStateTransition(StateMachineTransitionEvent $event) {&nbsp; &nbsp; &nbsp; &nbsp; // Since you are only interested in order transitions&nbsp; &nbsp; &nbsp; &nbsp; if ($event->getEntityName() !== OrderTransactionDefinition::ENTITY_NAME) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $orderTransactionsStatePaidId = $this->getOrderTransactionsStatePaidId($event->getContext());&nbsp; &nbsp; &nbsp; &nbsp; if ($orderTransactionsStatePaidId === null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Check if it was transitioned to paid&nbsp; &nbsp; &nbsp; &nbsp; if ($event->getToPlace()->getId() !== $orderTransactionsStatePaidId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Transaction was changed to paid do your thing&nbsp; &nbsp; &nbsp; &nbsp; $order = $this->getOrder($event->getEntityId(), $event->getContext());&nbsp; &nbsp; }&nbsp; &nbsp; private function getOrderTransactionsStatePaidId(Context $context): ?string {&nbsp; &nbsp; &nbsp; &nbsp; $criteria = new Criteria();&nbsp; &nbsp; &nbsp; &nbsp; // Add filter for OrderTransactionStateMachine&nbsp; &nbsp; &nbsp; &nbsp; $criteria->addFilter(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EqualsFilter('stateMachine.technicalName', \sprintf('%s.state', OrderTransactionDefinition::ENTITY_NAME)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EqualsFilter('technicalName', OrderTransactionStates::STATE_PAID)&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; return $this->stateMachineStateRepository->searchIds($criteria, $context)->firstId();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private function getOrder(string $orderTransactionId, Context $context): ?OrderEntity&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $criteria = new Criteria();&nbsp; &nbsp; &nbsp; &nbsp; $criteria->addFilter(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EqualsFilter('transactions.id', $orderTransactionId)&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $this->orderRepository->search($criteria, $context)->first();&nbsp; &nbsp; }}在您的 services xml 中添加如下内容。当然你需要调整FQCN(FullyQualifiedClassName)。<service id="Your\Namespace\OrderStateSubscriber">&nbsp; &nbsp; <argument type="service" id="state_machine_state.repository"/>&nbsp; &nbsp; <argument type="service" id="order.repository"/>&nbsp; &nbsp; <tag name="kernel.event_subscriber"/></service>
打开App,查看更多内容
随时随地看视频慕课网APP