vendor/store.shopware.com/viob2blogin/src/Core/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioB2BLogin\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 VioB2BLogin\Core\Checkout\Cart\Event\CheckoutEmployeeOrderPlacedEvent;
  10. use VioB2BLogin\Entity\Employee\EmployeeEntity;
  11. use VioB2BLogin\VioB2BLogin;
  12. class OrderPlacedSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $employeeRepository;
  15.     private EventDispatcherInterface $eventDispatcher;
  16.     public function __construct(
  17.         EntityRepositoryInterface $employeeRepository,
  18.         EventDispatcherInterface  $eventDispatcher
  19.     )
  20.     {
  21.         $this->employeeRepository $employeeRepository;
  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(VioB2BLogin::EMPLOYEE_FIELD$orderCustomFields)) {
  39.             // load Employee
  40.             $employeeId $orderCustomFields[VioB2BLogin::EMPLOYEE_FIELD];
  41.             $criteria = new Criteria([$employeeId]);
  42.             $criteria->addAssociation('salutation')
  43.                      ->addAssociation('customer.salutation');
  44.             $employee $this->employeeRepository->search($criteria$event->getContext())->first();
  45.             if ($employee instanceof EmployeeEntity) {
  46.                 $event->getOrder()->addExtension('employee'$employee);
  47.                 $this->eventDispatcher->dispatch(new CheckoutEmployeeOrderPlacedEvent(
  48.                     $event->getContext(),
  49.                     $event->getOrder(),
  50.                     $employee,
  51.                     $event->getSalesChannelId()
  52.                 ));
  53.             }
  54.         }
  55.     }
  56. }