英文:
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 => 'OnOrderReceived',
OrderEvents::ORDER_TRANSACTION_STATE_WRITTEN_EVENT => 'onMent',
];
}
I tried to log the data in $event
public function onMent(EntityWrittenEvent $event): void
{
highlight_string("<?php\n$data =\n" . var_export($event, true) . ";\n?>");
}
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
。
<service id="SwagBasicExample\Listener\OrderStateChangeEventListener">
<argument type="service" id="order_transaction.repository"/>
<tag name="kernel.event_subscriber"/>
</service>
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 [
'state_machine.order_transaction.state_changed' => 'onOrderTransactionStateChange',
];
}
public function onOrderTransactionStateChange(StateMachineStateChangeEvent $event): void
{
if ($event->getNextState()->getTechnicalName() !== StateMachineTransitionActions::ACTION_PAID) {
return;
}
$orderTransactionId = $event->getTransition()->getEntityId();
$criteria = new Criteria([$orderTransactionId]);
$criteria->addAssociation('paymentMethod');
/** @var OrderTransactionEntity $orderTransaction */
$orderTransaction = $this->transactionRepository
->search($criteria, $event->getContext())
->first();
if ($orderTransaction?->getPaymentMethod()?->getHandlerIdentifier() !== InvoicePayment::class) {
return;
}
// 如果交易状态进入了 `paid`,并且支付方式是发票,执行一些操作
}
}
英文:
You can add a subscriber and listen to state_machine.order_transaction.state_changed
.
<service id="SwagBasicExample\Listener\OrderStateChangeEventListener">
<argument type="service" id="order_transaction.repository"/>
<tag name="kernel.event_subscriber"/>
</service>
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 [
'state_machine.order_transaction.state_changed' => 'onOrderTransactionStateChange',
];
}
public function onOrderTransactionStateChange(StateMachineStateChangeEvent $event): void
{
if ($event->getNextState()->getTechnicalName() !== StateMachineTransitionActions::ACTION_PAID) {
return;
}
$orderTransactionId = $event->getTransition()->getEntityId();
$criteria = new Criteria([$orderTransactionId]);
$criteria->addAssociation('paymentMethod');
/** @var OrderTransactionEntity $orderTransaction */
$orderTransaction = $this->transactionRepository
->search($criteria, $event->getContext())
->first();
if ($orderTransaction?->getPaymentMethod()?->getHandlerIdentifier() !== InvoicePayment::class) {
return;
}
// do something if transaction state entered is `paid` and payment method is invoice
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论