<?php
declare(strict_types=1);
namespace K3n\Merchant\Subscriber\Checkout;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CheckoutOrderPlaced implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var EntityRepositoryInterface
*/
private $orderRepository;
/**
* @param EntityRepositoryInterface $orderRepository
* @param RequestStack $requestStack
*/
public function __construct(EntityRepositoryInterface $orderRepository, RequestStack $requestStack)
{
$this->orderRepository = $orderRepository;
$this->requestStack = $requestStack;
}
/**
*
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced'
];
}
/**
* @param CheckoutOrderPlacedEvent $event
* @return void
*/
public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
$order = $event->getOrder();
$merchantInput = $this->requestStack->getCurrentRequest()->request->get('merchantInput');
$merchantId = $this->requestStack->getCurrentRequest()->request->get('merchantId');
$customFields = $order->getCustomFields();
if($customFields === null) {
$order->setCustomFields(
[
'merchantInput' => $merchantInput,
'merchantId' => $merchantId
]
);
} else {
$customFields['merchantInput'] = $merchantInput;
$customFields['merchantId'] = $merchantId;
$order->setCustomFields($customFields);
}
$this->orderRepository->update(
[[
'id' => $order->getId(),
'customFields' => [
'merchantInput' => $merchantInput,
'merchantId' => $merchantId
]
]],
$event->getContext()
);
}
}