<?php
namespace EnetPickupDate\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\HttpFoundation\RequestStack;
class CheckoutSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepository
*/
protected $orderRepository;
/** @var RequestStack */
protected $requestStack;
public function __construct(EntityRepository $orderRepository, RequestStack $requestStack)
{
$this->orderRepository = $orderRepository;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced'
];
}
public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
$order = $event->getOrder();
if(!$order) {
return;
}
$request = $this->requestStack->getCurrentRequest()->request;
$pickup = $request->get('enet_pickup');
if(isset($pickup['date']) && isset($pickup['time'])) {
$pickupDate = $pickup['date'];
$pickupTime = $pickup['time'];
$this->orderRepository->update([
[
'id' => $order->getid(),
'customFields' => ['enet_pickupdate' => $pickupDate .'|'. $pickupTime]
]
], $event->getContext());
}
}
}