vendor/store.shopware.com/jlauloginascustomer/src/Subscriber/CheckoutOrderPlaced.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Jlau\LoginAsCustomer\Subscriber;
  3. use Jlau\LoginAsCustomer\Controller\LoginAsCustomer;
  4. use Jlau\LoginAsCustomer\LoginAsCustomer\LoginAsCustomerEntity;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class CheckoutOrderPlaced implements EventSubscriberInterface
  11. {
  12.     private RequestStack $requestStack;
  13.     private EntityRepositoryInterface $orderRepository;
  14.     public function __construct(RequestStack $requestStackEntityRepositoryInterface $orderRepository)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         $this->orderRepository $orderRepository;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [CheckoutOrderPlacedEvent::class => 'blameLoggedInUserForOrder'];
  22.     }
  23.     public function blameLoggedInUserForOrder(CheckoutOrderPlacedEvent $event)
  24.     {
  25.         try {
  26.             $session $this->requestStack->getSession();
  27.         } catch (SessionNotFoundException $e) {
  28.             return;
  29.         }
  30.         if (!$session->has(LoginAsCustomer::SESSION_NAME)) {
  31.             return;
  32.         }
  33.         /** @var LoginAsCustomerEntity $loginAsCustomer */
  34.         $loginAsCustomer $session->get(LoginAsCustomer::SESSION_NAME);
  35.         $orderId $event->getOrderId();
  36.         $customFields $event->getOrder()->getCustomFields();
  37.         $customFields['login-as-customer-user'] = $loginAsCustomer->getUserId();
  38.         $order = [[
  39.             'id' => $orderId,
  40.             'customFields' => $customFields
  41.         ]];
  42.         $this->orderRepository->update($order$event->getContext());
  43.     }
  44. }