亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Shopware6:訂單支付后訂閱哪個活動?

Shopware6:訂單支付后訂閱哪個活動?

PHP
梵蒂岡之花 2023-07-08 16:23:46
我想知道如果我想在客戶創建訂單后付款時觸發某個功能,應該使用哪個事件。我已經嘗試過這個: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');        }}
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

我為此使用 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 {


    /**

     * @var EntityRepositoryInterface

     */

    private $stateMachineStateRepository;


    /**

     * @var EntityRepositoryInterface

     */

    private $orderRepository;


    public function __construct(EntityRepositoryInterface $stateMachineStateRepository, EntityRepositoryInterface $orderRepository)

    {

        $this->stateMachineStateRepository = $stateMachineStateRepository;

        $this->orderRepository = $orderRepository;

    }


    public static function getSubscribedEvents(): array

    {

        return [

            StateMachineTransitionEvent::class => 'onStateTransition'

        ];

    }


    public function onStateTransition(StateMachineTransitionEvent $event) {

        // Since you are only interested in order transitions

        if ($event->getEntityName() !== OrderTransactionDefinition::ENTITY_NAME) {

            return;

        }


        $orderTransactionsStatePaidId = $this->getOrderTransactionsStatePaidId($event->getContext());

        if ($orderTransactionsStatePaidId === null) {

            return;

        }


        // Check if it was transitioned to paid

        if ($event->getToPlace()->getId() !== $orderTransactionsStatePaidId) {

            return;

        }


        // Transaction was changed to paid do your thing

        $order = $this->getOrder($event->getEntityId(), $event->getContext());

    }


    private function getOrderTransactionsStatePaidId(Context $context): ?string {

        $criteria = new Criteria();

        // Add filter for OrderTransactionStateMachine

        $criteria->addFilter(

            new EqualsFilter('stateMachine.technicalName', \sprintf('%s.state', OrderTransactionDefinition::ENTITY_NAME)),

            new EqualsFilter('technicalName', OrderTransactionStates::STATE_PAID)

        );


        return $this->stateMachineStateRepository->searchIds($criteria, $context)->firstId();

    }

    

    private function getOrder(string $orderTransactionId, Context $context): ?OrderEntity

    {

        $criteria = new Criteria();

        $criteria->addFilter(

            new EqualsFilter('transactions.id', $orderTransactionId)

        );

        

        return $this->orderRepository->search($criteria, $context)->first();

    }

}

在您的 services xml 中添加如下內容。當然你需要調整FQCN(FullyQualifiedClassName)。


<service id="Your\Namespace\OrderStateSubscriber">

    <argument type="service" id="state_machine_state.repository"/>

    <argument type="service" id="order.repository"/>

    <tag name="kernel.event_subscriber"/>

</service>


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號