custom/plugins/WbfkMultipleDeliveries/src/Subscriber/MailBeforeValidateEventSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\MultipleDeliveries\Subscriber;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryStates;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderDeliveryPosition\OrderDeliveryPositionCollection;
  7. use Shopware\Core\Checkout\Order\OrderEntity;
  8. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class MailBeforeValidateEventSubscriber implements EventSubscriberInterface
  16. {
  17.     public const OPEN_STATES = [OrderDeliveryStates::STATE_OPENOrderDeliveryStates::STATE_PARTIALLY_SHIPPED];
  18.     public function __construct(private readonly EntityRepository $deliveryPositionRepository)
  19.     {
  20.     }
  21.     /**
  22.      * @inheritDoc
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             MailBeforeValidateEvent::class => 'addShippedDeliveryToTemplateData',
  28.         ];
  29.     }
  30.     public function addShippedDeliveryToTemplateData(MailBeforeValidateEvent $event): void
  31.     {
  32.         if (($event->getTemplateData()['eventName'] ?? null) !== 'state_enter.order_delivery.state.shipped') {
  33.             return;
  34.         }
  35.         $shippedDeliveryId $event->getContext()->getExtension('deliveryInformation')?->get('deliveryId') ?? null;
  36.         /** @var OrderEntity $order */
  37.         $order $event->getTemplateData()['order'];
  38.         $shippedDelivery $shippedDeliveryId $order->getDeliveries()->get($shippedDeliveryId) : $order->getDeliveries()->first();
  39.         $this->addDeliveryPositions($shippedDelivery$event->getContext());
  40.         $event->addTemplateData('shippedDelivery'$shippedDelivery);
  41.         $event->addTemplateData('isAllDeliveriesShipped'$this->isAllDeliveriesShipped($order));
  42.     }
  43.     private function addDeliveryPositions(OrderDeliveryEntity $deliveryContext $context): void
  44.     {
  45.         $criteria = new Criteria();
  46.         $criteria->addAssociation('orderLineItem.product');
  47.         $criteria->getAssociation('orderLineItem')->addAssociation('cover');
  48.         $criteria->addSorting(new FieldSorting('createdAt'));
  49.         $criteria->addFilter(new EqualsFilter('orderDeliveryId'$delivery->getId()));
  50.         /** @var OrderDeliveryPositionCollection $positions */
  51.         $positions $this->deliveryPositionRepository->search($criteria$context)->getEntities();
  52.         $delivery->setPositions($positions);
  53.     }
  54.     private function isAllDeliveriesShipped(OrderEntity $order): bool
  55.     {
  56.         /** @var OrderDeliveryEntity $delivery */
  57.         foreach ($order->getDeliveries() as $delivery) {
  58.             if (in_array($delivery->getStateMachineState()->getTechnicalName(), self::OPEN_STATES)) {
  59.                 return false;
  60.             }
  61.         }
  62.         return true;
  63.     }
  64. }