custom/plugins/webwinkelkeur/src/Listener/OrderListener.php line 32

Open in your IDE?
  1. <?php
  2. namespace WebwinkelKeur\Shopware\Listener;
  3. use Shopware\Core\Checkout\Cart\Exception\OrderNotFoundException;
  4. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use WebwinkelKeur\Shopware\Service\InvitationService;
  10. class OrderListener {
  11.     /**
  12.      * @var InvitationService
  13.      */
  14.     private InvitationService $invitationService;
  15.     /**
  16.      * @var EntityRepository
  17.      */
  18.     private EntityRepository $orderRepository;
  19.     public function __construct(
  20.         EntityRepository $order_repository,
  21.         InvitationService $invitation_service
  22.     ) {
  23.         $this->orderRepository $order_repository;
  24.         $this->invitationService $invitation_service;
  25.     }
  26.     public function onOrderCompleted(OrderStateMachineStateChangeEvent $event): void {
  27.         $context $event->getContext();
  28.         $order $this->getOrder($event->getOrder()->getUniqueIdentifier(), $context);
  29.         $this->invitationService->sendInvitation($order$context);
  30.     }
  31.     /**
  32.      * @throws OrderNotFoundException
  33.      */
  34.     private function getOrder(string $order_idContext $context): OrderEntity {
  35.         $order_criteria $this->getOrderCriteria($order_id);
  36.         /** @var OrderEntity|null $order */
  37.         $order $this->orderRepository->search($order_criteria$context)->first();
  38.         if ($order === null) {
  39.             throw new OrderNotFoundException($order_id);
  40.         }
  41.         return $order;
  42.     }
  43.     private function getOrderCriteria(string $order_id): Criteria {
  44.         $order_criteria = new Criteria([$order_id]);
  45.         $order_criteria->addAssociation('orderCustomer.customer');
  46.         $order_criteria->addAssociation('language.locale');
  47.         return $order_criteria;
  48.     }
  49. }