custom/static-plugins/CioApprovalThreshold/src/Subscriber/OrderPlacedSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace CioApprovalThreshold\Subscriber;
  3. use CioApprovalThreshold\Event\ReleaserHasToVerifyOrderEvent;
  4. use CioApprovalThreshold\States\ApprovalStates;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Order\OrderDefinition;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  15. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  16. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  17. use Shopware\Core\System\StateMachine\Transition;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\Routing\Router;
  22. use Symfony\Component\Routing\RouterInterface;
  23. class OrderPlacedSubscriber implements EventSubscriberInterface
  24. {
  25.     private EventDispatcherInterface $eventDispatcher;
  26.     private RequestStack $requestStack;
  27.     private EntityRepository $orderRepository;
  28.     private EntityRepository $customerRepository;
  29.     private StateMachineRegistry $stateMachineRegistry;
  30.     private AbstractSalesChannelContextFactory $salesChannelContextFactory;
  31.     private RouterInterface $router;
  32.     public function __construct(EventDispatcherInterface $eventDispatcherRequestStack $requestStackEntityRepository $orderRepositoryEntityRepository $customerRepositoryStateMachineRegistry $stateMachineRegistryAbstractSalesChannelContextFactory $salesChannelContextFactoryRouterInterface $router)
  33.     {
  34.         $this->eventDispatcher $eventDispatcher;
  35.         $this->requestStack $requestStack;
  36.         $this->orderRepository $orderRepository;
  37.         $this->customerRepository $customerRepository;
  38.         $this->stateMachineRegistry $stateMachineRegistry;
  39.         $this->salesChannelContextFactory $salesChannelContextFactory;
  40.         $this->router $router;
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent'
  46.         ];
  47.     }
  48.     public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event)
  49.     {
  50.         $order $event->getOrder();
  51.         $request $this->requestStack->getCurrentRequest();
  52.         $releaseCustomerId $request->request->get('cioSelectedReleaseCustomerId');
  53.         if (is_string($releaseCustomerId) && Uuid::isValid($releaseCustomerId)) {
  54.             $criteria = new Criteria([$releaseCustomerId]);
  55.             $releaseCustomer $this->customerRepository->search($criteria$event->getContext())->first();
  56.             if ($releaseCustomer instanceof CustomerEntity) {
  57.                 // load current version of order from repository
  58.                 $criteria = new Criteria();
  59.                 $criteria->addFilter(new EqualsFilter('id'$order->getId()));
  60.                 $orderForCustomFields $this->orderRepository->search($criteria$event->getContext())->first();
  61.                 $customFields = [];
  62.                 if ($orderForCustomFields instanceof OrderEntity && is_array($orderForCustomFields->getCustomFields())) {
  63.                     $customFields $orderForCustomFields->getCustomFields();
  64.                 }
  65.                 $customFields['cioApprovalThresholdReleaseCustomerFirstName'] = $releaseCustomer->getFirstName();
  66.                 $customFields['cioApprovalThresholdReleaseCustomerLastName'] = $releaseCustomer->getLastName();
  67.                 // save on checkout confirm page selected customer that is responsible for releasing this new order
  68.                 $this->orderRepository->update([
  69.                     [
  70.                         'id' => $order->getId(),
  71.                         'approvalThresholdExtension' => [
  72.                             'selectedReleaseCustomerId' => $releaseCustomer->getId()
  73.                         ],
  74.                         'customFields' => $customFields
  75.                     ]
  76.                 ], $event->getContext());
  77.                 $order->setCustomFields($customFields);
  78.                 // change state of order from "open" to "needs release"
  79.                 $salesChannelContext $this->salesChannelContextFactory->create(''$order->getSalesChannelId(), [SalesChannelContextService::LANGUAGE_ID => $order->getLanguageId()]);
  80.                 $this->stateMachineRegistry->transition(
  81.                     new Transition(
  82.                         OrderDefinition::ENTITY_NAME,
  83.                         $order->getId(),
  84.                         ApprovalStates::NEEDS_APPROVAL_TRANSITION_NAME,
  85.                         'stateId'
  86.                     ),
  87.                     $salesChannelContext->getContext()
  88.                 );
  89.                 // customer that is responsible for releasing this new order must get a mail with url that allows verification of the order, event is available in flow builder
  90.                 $confirmUrl $this->router->generate('storefront.cioapprovalthreshold.verify.page', [
  91.                     'orderId' => $order->getId()
  92.                 ], Router::ABSOLUTE_URL);
  93.                 $event = new ReleaserHasToVerifyOrderEvent($event->getContext(), $order$order->getSalesChannelId(), $confirmUrl, new MailRecipientStruct([$releaseCustomer->getEmail() => $releaseCustomer->getEmail()]));
  94.                 $this->eventDispatcher->dispatch($event);
  95.             }
  96.         }
  97.     }
  98. }