custom/plugins/b2bsellerscore/src/Components/Order/Subscriber/OrderPlacedSubscriber.php line 85

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Order\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedCriteriaEvent;
  5. use B2bSellersCore\Components\Order\Service\OrderMailRecipientServiceInterface;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class OrderPlacedSubscriber implements EventSubscriberInterface
  11. {
  12.     use B2bContextTrait;
  13.     private EntityRepository $customerRepository;
  14.     private EntityRepository $employeeRepository;
  15.     private OrderMailRecipientServiceInterface $orderMailRecipientService;
  16.     public function __construct(
  17.         EntityRepository                   $customerRepository,
  18.         EntityRepository                   $employeeRepository,
  19.         OrderMailRecipientServiceInterface $orderMailRecipientService
  20.     )
  21.     {
  22.         $this->customerRepository $customerRepository;
  23.         $this->employeeRepository $employeeRepository;
  24.         $this->orderMailRecipientService $orderMailRecipientService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             CheckoutOrderPlacedEvent::class => [
  30.                 ['replaceMailRecipients', -10],
  31.                 ['addOrderExtensions'],
  32.                 ['addEmployeeData']
  33.             ],
  34.             CheckoutOrderPlacedCriteriaEvent::class => 'addB2bExtensionAssociations'
  35.         ];
  36.     }
  37.     public function replaceMailRecipients(CheckoutOrderPlacedEvent $event): void
  38.     {
  39.         $recipients $this->orderMailRecipientService->getRecipientsByOrder($event->getOrder(), $event->getContext(), true);
  40.         $event->getMailStruct()->setRecipients($recipients->getRecipients());
  41.     }
  42.     public function addOrderExtensions(CheckoutOrderPlacedEvent $event): void
  43.     {
  44.         $customFields $event->getOrder()->getCustomFields();
  45.         if (empty($customFields['b2b_order_sales_representative_id'])) {
  46.             return;
  47.         }
  48.         $customer $this->customerRepository->search(new Criteria([$customFields['b2b_order_sales_representative_id']]), $event->getContext())->first();
  49.         if (empty($customer)) {
  50.             return;
  51.         }
  52.         $event->getOrder()->addExtension('salesRepresentative'$customer);
  53.     }
  54.     public function addEmployeeData(CheckoutOrderPlacedEvent $event): void
  55.     {
  56.         $customFields $event->getOrder()->getCustomFields();
  57.         if (empty($customFields['b2b_order_customer_employee_id'])) {
  58.             return;
  59.         }
  60.         $employee $this->employeeRepository->search(new Criteria([$customFields['b2b_order_customer_employee_id']]), $event->getContext())->first();
  61.         if (empty($employee)) {
  62.             return;
  63.         }
  64.         $event->getOrder()->getOrderCustomer()->addExtension('employee'$employee);
  65.     }
  66.     public function addB2bExtensionAssociations(CheckoutOrderPlacedCriteriaEvent $event)
  67.     {
  68.         $event->getCriteria()->addAssociations([
  69.             'b2bOrderExtension.employee',
  70.             'b2bOrderExtension.salesRepresentative'
  71.         ]);
  72.     }
  73. }