<?php
namespace B2bSellersCore\Components\Order\Subscriber;
use B2bSellersCore\Components\Order\Service\OrderMailRecipientServiceInterface;
use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Order\Event\OrderStateChangeCriteriaEvent;
class OrderStateChangedSubscriber implements EventSubscriberInterface
{
private OrderMailRecipientServiceInterface $orderMailRecipientService;
public function __construct(
OrderMailRecipientServiceInterface $orderMailRecipientService
)
{
$this->orderMailRecipientService = $orderMailRecipientService;
}
public static function getSubscribedEvents(): array
{
return [
OrderStateChangeCriteriaEvent::class => 'addB2bExtensionAssociations',
OrderStateMachineStateChangeEvent::class => [['replaceMailRecipients', -10]]
];
}
public function replaceMailRecipients(OrderStateMachineStateChangeEvent $event): void
{
$recipients = $this->orderMailRecipientService->getRecipientsByOrder($event->getOrder(), $event->getContext());
$event->getMailStruct()->setRecipients($recipients->getRecipients());
}
public function addB2bExtensionAssociations(OrderStateChangeCriteriaEvent $event)
{
$event->getCriteria()->addAssociations([
'b2bOrderExtension.employee',
'b2bOrderExtension.salesRepresentative'
]);
}
}