可以使用什么事件来检查在Shopware 6.5上刚刚支付的订单状态?

huangapple go评论84阅读模式
英文:

what event can be used to check the state of an order that has just been paid for on shopware 6.5

问题

你好,我已经开发了一个插件,正在尝试在客户在线支付时获取订单的状态。

我目前正在尝试以下代码:

use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;

public static function getSubscribedEvents(): array
{
    return [
        CheckoutOrderPlacedEvent::class => 'OnOrderReceived',
        OrderEvents::ORDER_TRANSACTION_STATE_WRITTEN_EVENT => 'onMent',
    ];
}

我尝试在$event中记录数据:

public function onMent(EntityWrittenEvent $event): void
{
    highlight_string("<?php\n$data =\n" . var_export($event, true) . ";\n?>");
}

但是没有显示任何内容。

在支付后如何检测订单的状态?

英文:

Hello I have developed a plugin and I am trying to get the state of an order when a customer makes an online payment.

I am currently trying the below

use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;

 public static function getSubscribedEvents(): array
{


       return [
CheckoutOrderPlacedEvent::class =&gt; &#39;OnOrderReceived&#39;,
            OrderEvents::ORDER_TRANSACTION_STATE_WRITTEN_EVENT =&gt; &#39;onMent&#39;,
        ];
}

I tried to log the data in $event

public function onMent(EntityWrittenEvent $event): void
    {
         highlight_string(&quot;&lt;?php\n$data =\n&quot; . var_export($event, true) . &quot;;\n?&gt;&quot;);
    }

but nothing comes up.

how can I detect the state of an order after payment has been made?

答案1

得分: 0

你可以添加一个订阅者并监听 state_machine.order_transaction.state_changed

&lt;service id=&quot;SwagBasicExample\Listener\OrderStateChangeEventListener&quot;&gt;
    &lt;argument type=&quot;service&quot; id=&quot;order_transaction.repository&quot;/&gt;
    &lt;tag name=&quot;kernel.event_subscriber&quot;/&gt;
&lt;/service&gt;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\InvoicePayment;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderStateChangeEventListener implements EventSubscriberInterface
{
    public function __construct(
        private readonly EntityRepository $transactionRepository
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            &#39;state_machine.order_transaction.state_changed&#39; =&gt; &#39;onOrderTransactionStateChange&#39;,
        ];
    }

    public function onOrderTransactionStateChange(StateMachineStateChangeEvent $event): void
    {
        if ($event-&gt;getNextState()-&gt;getTechnicalName() !== StateMachineTransitionActions::ACTION_PAID) {
            return;
        }

        $orderTransactionId = $event-&gt;getTransition()-&gt;getEntityId();

        $criteria = new Criteria([$orderTransactionId]);
        $criteria-&gt;addAssociation(&#39;paymentMethod&#39;);

        /** @var OrderTransactionEntity $orderTransaction */
        $orderTransaction = $this-&gt;transactionRepository
            -&gt;search($criteria, $event-&gt;getContext())
            -&gt;first();

        if ($orderTransaction?-&gt;getPaymentMethod()?-&gt;getHandlerIdentifier() !== InvoicePayment::class) {
            return;
        }

        // 如果交易状态进入了 `paid`,并且支付方式是发票,执行一些操作
    }
}
英文:

You can add a subscriber and listen to state_machine.order_transaction.state_changed.

&lt;service id=&quot;SwagBasicExample\Listener\OrderStateChangeEventListener&quot;&gt;
    &lt;argument type=&quot;service&quot; id=&quot;order_transaction.repository&quot;/&gt;
    &lt;tag name=&quot;kernel.event_subscriber&quot;/&gt;
&lt;/service&gt;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\InvoicePayment;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderStateChangeEventListener implements EventSubscriberInterface
{
    public function __construct(
        private readonly EntityRepository $transactionRepository
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            &#39;state_machine.order_transaction.state_changed&#39; =&gt; &#39;onOrderTransactionStateChange&#39;,
        ];
    }

    public function onOrderTransactionStateChange(StateMachineStateChangeEvent $event): void
    {
        if ($event-&gt;getNextState()-&gt;getTechnicalName() !== StateMachineTransitionActions::ACTION_PAID) {
            return;
        }

        $orderTransactionId = $event-&gt;getTransition()-&gt;getEntityId();

        $criteria = new Criteria([$orderTransactionId]);
        $criteria-&gt;addAssociation(&#39;paymentMethod&#39;);

        /** @var OrderTransactionEntity $orderTransaction */
        $orderTransaction = $this-&gt;transactionRepository
            -&gt;search($criteria, $event-&gt;getContext())
            -&gt;first();

        if ($orderTransaction?-&gt;getPaymentMethod()?-&gt;getHandlerIdentifier() !== InvoicePayment::class) {
            return;
        }

        // do something if transaction state entered is `paid` and payment method is invoice
    }
}

huangapple
  • 本文由 发表于 2023年7月20日 18:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定