<?php declare(strict_types=1);
namespace GrimmTheme\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
class OrderSubscriber implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
private $orderRepository;
public function __construct(RequestStack $requestStack, EntityRepositoryInterface $orderRepository) {
$this->requestStack = $requestStack;
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
];
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
{
$currentRequest = $this->requestStack->getCurrentRequest();
$bestellreferenz = $currentRequest->get("bestellreferenz");
$order = $event->getOrder();
$id = $order->getId();
$this->orderRepository->update([
['id' => $id, 'customFields' => ['bestellreferenz' => $bestellreferenz] ]
]
,$event->getContext()
);
}
}