<?php
namespace K3nExtendVioRepresentativeLogin\Subscriber;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class AccountOrderPageSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $stateMachineTransitionRepository;
public function __construct(EntityRepositoryInterface $stateMachineTransitionRepository)
{
$this->stateMachineTransitionRepository = $stateMachineTransitionRepository;
}
public static function getSubscribedEvents(): array
{
return [
AccountOrderPageLoadedEvent::class => 'onAccountOrderDetailPageLoaded'
];
}
public function onAccountOrderDetailPageLoaded(AccountOrderPageLoadedEvent $event): void
{
$orders = $event->getPage()->getOrders()->getEntities();
$context = $event->getContext();
$stateMachineTransitions = [];
/** @var OrderEntity $order */
foreach ($orders as $order) {
$orderStateId = $order->getStateId();
if(!isset($stateMachineTransitions[$orderStateId])) {
$criteria = new Criteria();
$criteria->addAssociation('toStateMachineState');
$criteria->addFilter(new EqualsFilter('fromStateId', $orderStateId));
$allowedStates = $this->stateMachineTransitionRepository->search($criteria, $context)->getEntities();
$stateMachineTransitions[$orderStateId] = $allowedStates;
}
$order->addExtension('stateMachineAllowedTransitions', $stateMachineTransitions[$orderStateId]);
$orders->add($order);
}
}
}