custom/static-plugins/K3nMerchant/src/Subscriber/Checkout/CheckoutOrderPlaced.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace K3n\Merchant\Subscriber\Checkout;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class CheckoutOrderPlaced implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var RequestStack
  12.      */
  13.     private $requestStack;
  14.     /**
  15.      * @var EntityRepositoryInterface
  16.      */
  17.     private $orderRepository;
  18.     /**
  19.      * @param EntityRepositoryInterface $orderRepository
  20.      * @param RequestStack $requestStack
  21.      */
  22.     public function __construct(EntityRepositoryInterface $orderRepositoryRequestStack $requestStack)
  23.     {
  24.         $this->orderRepository $orderRepository;
  25.         $this->requestStack    $requestStack;
  26.     }
  27.     /**
  28.      *
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced'
  35.         ];
  36.     }
  37.     /**
  38.      * @param CheckoutOrderPlacedEvent $event
  39.      * @return void
  40.      */
  41.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  42.     {
  43.         $order         $event->getOrder();
  44.         $merchantInput $this->requestStack->getCurrentRequest()->request->get('merchantInput');
  45.         $merchantId $this->requestStack->getCurrentRequest()->request->get('merchantId');
  46.         $customFields $order->getCustomFields();
  47.         if($customFields === null) {
  48.             $order->setCustomFields(
  49.                 [
  50.                     'merchantInput' => $merchantInput,
  51.                     'merchantId' => $merchantId
  52.                 ]
  53.             );
  54.         } else {
  55.             $customFields['merchantInput'] = $merchantInput;
  56.             $customFields['merchantId']    = $merchantId;
  57.             $order->setCustomFields($customFields);
  58.         }
  59.         $this->orderRepository->update(
  60.             [[
  61.                 'id' => $order->getId(),
  62.                 'customFields' => [
  63.                     'merchantInput' => $merchantInput,
  64.                     'merchantId' => $merchantId
  65.                 ]
  66.             ]],
  67.             $event->getContext()
  68.         );
  69.     }
  70. }