<?php
declare(strict_types=1);
namespace MadOrderCustomerDatatransfer\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class OrderSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $orderRepository;
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
EntityRepositoryInterface $orderRepository,
RequestStack $requestStack
) {
$this->orderRepository = $orderRepository;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => [
['addCustomField', 99],
]
];
}
/**
* @param CheckoutOrderPlacedEvent $event
*/
public function addCustomField(CheckoutOrderPlacedEvent $event): void
{
/* in the request the custom fields are avaible */
$request = $this->requestStack->getCurrentRequest()->request;
$order = $event->getOrder();
$customFields = $order->getCustomFields() ?: [];;
if($request->get('datatransfer') == 'on'){
$customFields = array_merge([
'mad_order_customer_datatransfer' => true
], $customFields);
}else{
$customFields = array_merge([
'mad_order_customer_datatransfer' => false
], $customFields);
}
if($request->get('shippingdatatransfer') == 'on'){
$customFields = array_merge([
'mad_order_customer_shippingdatatransfer' => true
], $customFields);
}else{
$customFields = array_merge([
'mad_order_customer_shippingdatatransfer' => false
], $customFields);
}
$this->orderRepository->update([[
'id' => $order->getId(),
'customFields' => $customFields
]], $event->getContext());
}
}