custom/plugins/MadOrderCustomerDatatransfer/src/Subscriber/OrderSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadOrderCustomerDatatransfer\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class OrderSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var EntityRepositoryInterface
  12.      */
  13.     private $orderRepository;
  14.     /**
  15.      * @var RequestStack
  16.      */
  17.     private $requestStack;
  18.     public function __construct(
  19.         EntityRepositoryInterface $orderRepository,
  20.         RequestStack $requestStack
  21.     ) {
  22.         $this->orderRepository $orderRepository;
  23.         $this->requestStack $requestStack;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CheckoutOrderPlacedEvent::class => [
  29.                 ['addCustomField'99],
  30.             ]
  31.         ];
  32.     }
  33.     /**
  34.      * @param CheckoutOrderPlacedEvent $event
  35.      */
  36.     public function addCustomField(CheckoutOrderPlacedEvent $event): void
  37.     {
  38.         /* in the request the custom fields are avaible */
  39.         $request $this->requestStack->getCurrentRequest()->request;
  40.         $order $event->getOrder();
  41.         $customFields $order->getCustomFields() ?: [];;
  42.         if($request->get('datatransfer') == 'on'){
  43.             $customFields array_merge([
  44.                 'mad_order_customer_datatransfer' => true
  45.             ], $customFields);
  46.         }else{
  47.             $customFields array_merge([
  48.                 'mad_order_customer_datatransfer' => false
  49.             ], $customFields);
  50.         }
  51.         if($request->get('shippingdatatransfer') == 'on'){
  52.             $customFields array_merge([
  53.                 'mad_order_customer_shippingdatatransfer' => true
  54.             ], $customFields);
  55.         }else{
  56.             $customFields array_merge([
  57.                 'mad_order_customer_shippingdatatransfer' => false
  58.             ], $customFields);
  59.         }
  60.         $this->orderRepository->update([[
  61.             'id' => $order->getId(),
  62.             'customFields' => $customFields
  63.         ]], $event->getContext());
  64.     }
  65. }