Search code examples
phpshopware6

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


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?


Solution

  • 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
        }
    }