custom/plugins/SwagBurg/src/Subscriber/OrderSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SwagBurg\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class OrderSubscriber implements EventSubscriberInterface
  9. {
  10.     private EntityRepositoryInterface $orderRepository;
  11.     private RequestStack $requestStack;
  12.     public function __construct(
  13.         EntityRepositoryInterface $orderRepository,
  14.         RequestStack $requestStack
  15.     ) {
  16.         $this->orderRepository $orderRepository;
  17.         $this->requestStack $requestStack;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
  23.         ];
  24.     }
  25.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  26.     {
  27.         $request $this->requestStack->getCurrentRequest();
  28.         if (!$request) {
  29.             return;
  30.         }
  31.         $customerComments $request->request->get('customer_comment');
  32.         if (empty($customerComments)) {
  33.             return;
  34.         }
  35.         // Update de order met de comments
  36.         $this->orderRepository->update([[
  37.             'id' => $event->getOrder()->getId(),
  38.             'customerComment' => $customerComments
  39.         ]], Context::createDefaultContext());
  40.     }
  41. }