<?php declare(strict_types=1);
namespace SwagBurg\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class OrderSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $orderRepository;
private RequestStack $requestStack;
public function __construct(
EntityRepositoryInterface $orderRepository,
RequestStack $requestStack
) {
$this->orderRepository = $orderRepository;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
];
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return;
}
$customerComments = $request->request->get('customer_comment');
if (empty($customerComments)) {
return;
}
// Update de order met de comments
$this->orderRepository->update([[
'id' => $event->getOrder()->getId(),
'customerComment' => $customerComments
]], Context::createDefaultContext());
}
}