vendor/store.shopware.com/viob2bworkflow/src/Core/Subscriber/CartSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Vio\B2BWorkflow\Core\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use VioB2BLogin\Core\Services\EmployeeService;
  11. use VioB2BLogin\Entity\Employee\EmployeeEntity;
  12. use VioB2BLogin\VioB2BLogin;
  13. use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutCustomerOrderRequestPlacedEvent;
  14. use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutEmployeeOrderRequestPlacedEvent;
  15. use Vio\B2BWorkflow\Core\ContextExtension;
  16. use Vio\B2BWorkflow\Services\StateService;
  17. class CartSubscriber implements EventSubscriberInterface
  18. {
  19.     private EmployeeService $employeeService;
  20.     private StateService $stateService;
  21.     private EventDispatcherInterface $eventDispatcher;
  22.     private EntityRepositoryInterface $employeeRepository;
  23.     public function __construct(
  24.         EmployeeService           $employeeService,
  25.         StateService              $stateService,
  26.         EventDispatcherInterface  $eventDispatcher,
  27.         EntityRepositoryInterface $employeeRepository
  28.     )
  29.     {
  30.         $this->employeeService $employeeService;
  31.         $this->stateService $stateService;
  32.         $this->eventDispatcher $eventDispatcher;
  33.         $this->employeeRepository $employeeRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CartConvertedEvent::class => 'onCartConverted',
  39.             CheckoutOrderPlacedEvent::class => ['onOrderPlaced'999]
  40.         ];
  41.     }
  42.     public function onCartConverted(CartConvertedEvent $event): void
  43.     {
  44.         $cart $event->getCart();
  45.         $convertedCart $event->getConvertedCart();
  46.         $salesChannelContext $event->getSalesChannelContext();
  47.         $employee $this->employeeService->getEmployeeByContext($salesChannelContext);
  48.         // check rights
  49.         if ($employee
  50.             && $cart->hasExtensionOfType(ContextExtension::KEYContextExtension::class)) {
  51.             $convertedCart['stateId'] = $this->stateService->getApprovalState($event->getContext())->getId();
  52.             foreach ($convertedCart['transactions'] as &$transaction) {
  53.                 $transaction['stateId'] = $this->stateService->getPresetState($event->getContext())->getId();
  54.             }
  55.             unset($transaction);
  56.             $extension = (new ContextExtension())->setActive(true);
  57.             $event->getContext()->addExtension(ContextExtension::KEY$extension);
  58.         }
  59.         $event->setConvertedCart($convertedCart);
  60.     }
  61.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  62.     {
  63.         $order $event->getOrder();
  64.         if ($order->getStateId() === $this->stateService->getApprovalState($event->getContext())->getId()) {
  65.             // when the order has an employee
  66.             $orderCustomFields $event->getOrder()->getCustomFields();
  67.             if (is_array($orderCustomFields)
  68.                 && array_key_exists(VioB2BLogin::EMPLOYEE_FIELD$orderCustomFields)) {
  69.                 // load Employee
  70.                 $employeeId $orderCustomFields[VioB2BLogin::EMPLOYEE_FIELD];
  71.                 $criteria = new Criteria([$employeeId]);
  72.                 $criteria->addAssociation('customer');
  73.                 $employee $this->employeeRepository->search($criteria$event->getContext())->first();
  74.                 if ($employee instanceof EmployeeEntity) {
  75.                     $this->eventDispatcher->dispatch(new CheckoutEmployeeOrderRequestPlacedEvent(
  76.                         $event->getContext(),
  77.                         $event->getOrder(),
  78.                         $employee,
  79.                         $event->getSalesChannelId()
  80.                     ));
  81.                     $this->eventDispatcher->dispatch(new CheckoutCustomerOrderRequestPlacedEvent(
  82.                         $event->getContext(),
  83.                         $event->getOrder(),
  84.                         $employee,
  85.                         $event->getSalesChannelId()
  86.                     ));
  87.                 }
  88.             }
  89.             $event->stopPropagation();
  90.         }
  91.     }
  92. }