How I can get last (actual) transaction status in Shopware 6

Viewed 66

I try to get actual transaction status in Shopware 6. I try to use this code $paymentStatus = $order->get('transactions')->first()->get('stateMachineState')->get('technicalName'); but here is a problem. transactions have many statuses inside array and if I choose first() sometimes it's wrong status. For example (it's real example) we have order. The order has 3 transaction statuses in time which a customer held:

  1. Open
  2. Cancelled
  3. Paid

if I use code which was mentioned above I get Cancelled status (because it's first element of array) but I expect Paid. I have only one solution it's sort array by transaction createdAt

1 Answers

The transaction with the highest creation time is the current transaction of an order. For example this is how Shopware determines the order payment status on the account orders page:

First transactions are sorted ascending (AccountOrderPageLoader):

$criteria
    ->getAssociation('transactions')
    ->addSorting(new FieldSorting('createdAt'));

Then the last transaction is used for the payment status of an order (order-item.html.twig):

<span class="order-table-body-value">{{ order.transactions|last.stateMachineState.translated.name }}</span>

You can do the same. Sort your transactions ascending and then get the status of the last one:

$paymentStatus = $order->getTransactions()->last()->getStateMachineState()->getTechnicalName();
Related