custom/plugins/VioRepresentativeLogin/src/Core/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioRepresentativeLogin\Core\Checkout\Cart\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  9. use VioRepresentativeLogin\Core\Checkout\Cart\Event\CheckoutAgentOrderPlacedEvent;
  10. use VioRepresentativeLogin\Entity\Agent\AgentEntity;
  11. use VioRepresentativeLogin\VioRepresentativeLogin;
  12. class OrderPlacedSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $agentRepository;
  15.     private EventDispatcherInterface $eventDispatcher;
  16.     public function __construct(
  17.         EntityRepositoryInterface $agentRepository,
  18.         EventDispatcherInterface  $eventDispatcher
  19.     )
  20.     {
  21.         $this->agentRepository $agentRepository;
  22.         $this->eventDispatcher $eventDispatcher;
  23.     }
  24.     /**
  25.      * @inheritDoc
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
  31.         ];
  32.     }
  33.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  34.     {
  35.         // when the order has an employee
  36.         $orderCustomFields $event->getOrder()->getCustomFields();
  37.         if (is_array($orderCustomFields)
  38.             && array_key_exists(VioRepresentativeLogin::AGENT_FIELD$orderCustomFields)) {
  39.             // load Employee
  40.             $agentId $orderCustomFields[VioRepresentativeLogin::AGENT_FIELD];
  41.             $agent $this->agentRepository->search((new Criteria([$agentId]))->addAssociation('salutation'), $event->getContext())->first();
  42.             if ($agent instanceof AgentEntity) {
  43.                 $event->getOrder()->addExtension('agent'$agent);
  44.                 $this->eventDispatcher->dispatch(new CheckoutAgentOrderPlacedEvent(
  45.                     $event->getContext(),
  46.                     $event->getOrder(),
  47.                     $agent,
  48.                     $event->getSalesChannelId()
  49.                 ));
  50.             }
  51.         }
  52.     }
  53. }