<?php declare(strict_types=1);
namespace FuerstenbergPorzellan\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class OrderSubscriber implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var EntityRepository
*/
private $orderRepository;
public function __construct(
RequestStack $requestStack,
EntityRepository $orderRepository
)
{
$this->requestStack = $requestStack;
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'CheckoutOrderPlaced',
];
}
public function CheckoutOrderPlaced(CheckoutOrderPlacedEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
if ($request !== null) {
$confirmdatasend = $request->request->get('confirmdatasend');
if ($confirmdatasend !== null) {
$order = $event->getOrder();
$customFields = $order->getCustomFields() ?? [];
$customFields['lds_checkout_privacy'] = filter_var($confirmdatasend, FILTER_VALIDATE_BOOLEAN);
$this->orderRepository->upsert([
[
'id' => $order->getId(),
'customFields' => $customFields
]
], $event->getContext());
}
}
}
}