custom/static-plugins/checkout/src/Subscriber/OrderSubscriber.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\Checkout\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  10. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  11. use Shopware\Core\System\CustomField\CustomFieldEntity;
  12. use Shopware\Core\System\CustomField\CustomFieldService;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\InputBag;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Wexo\Checkout\Core\Checkout\Cart\SalesChannel\CartOrderRouteDecorator;
  18. use Wexo\Checkout\Helper\Helper;
  19. use Wexo\Checkout\Struct\CheckoutOrderData;
  20. /**
  21.  * Class OrderSubscriber
  22.  * @package Wexo\Checkout\Subscriber
  23.  */
  24. class OrderSubscriber implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var Helper
  28.      */
  29.     protected $helper;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     protected $orderRepository;
  34.     /**
  35.      * @var CustomFieldService
  36.      */
  37.     protected $customFieldService;
  38.     /**
  39.      * @var DefinitionInstanceRegistry
  40.      */
  41.     protected $definitionRegistry;
  42.     /**
  43.      * @var RequestStack
  44.      */
  45.     protected $requestStack;
  46.     /**
  47.      * OrderSubscriber constructor.
  48.      * @param Helper $helper
  49.      * @param EntityRepositoryInterface $orderRepository
  50.      * @param CustomFieldService $customFieldService
  51.      * @param DefinitionInstanceRegistry $definitionRegistry
  52.      * @param RequestStack $requestStack
  53.      */
  54.     public function __construct(
  55.         Helper $helper,
  56.         EntityRepositoryInterface $orderRepository,
  57.         CustomFieldService $customFieldService,
  58.         DefinitionInstanceRegistry $definitionRegistry,
  59.         RequestStack $requestStack
  60.     ) {
  61.         $this->helper $helper;
  62.         $this->orderRepository $orderRepository;
  63.         $this->customFieldService $customFieldService;
  64.         $this->definitionRegistry $definitionRegistry;
  65.         $this->requestStack $requestStack;
  66.     }
  67.     /**
  68.      * @return string[]
  69.      */
  70.     public static function getSubscribedEvents(): array
  71.     {
  72.         return [
  73.             CheckoutOrderPlacedEvent::class => [
  74.                 ['onOrderPlaced'100]
  75.             ]
  76.         ];
  77.     }
  78.     /**
  79.      * @param CheckoutOrderPlacedEvent $event
  80.      */
  81.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  82.     {
  83.         $request $this->requestStack->getCurrentRequest();
  84.         if ($request && $inputBag $request->request) {
  85.             $this->addCustomFields(
  86.                 $inputBag->get('customFields', []),
  87.                 $event->getOrder(),
  88.                 $event->getContext()
  89.             );
  90.         }
  91.     }
  92.     /**
  93.      * @param array $customFields
  94.      * @param OrderEntity $order
  95.      * @param Context $context
  96.      */
  97.     private function addCustomFields(
  98.         array $customFields,
  99.         OrderEntity $order,
  100.         Context $context
  101.     ): void {
  102.         if (!$customFields || empty($customFields)) {
  103.             return;
  104.         }
  105.         $customFields array_merge($order->getCustomFields() ?? [], $customFields);
  106.         $exceptions = [];
  107.         // Decode all customField values
  108.         foreach ($customFields as $key => &$value) {
  109.             $customField $this->customFieldService->getCustomField($key);
  110.             if (!$customField) {
  111.                 continue;
  112.             }
  113.             try {
  114.                 $customField->compile($this->definitionRegistry);
  115.                 // Decode based on shopware type
  116.                 $value $customField->getSerializer()->decode($customField$value);
  117.             } catch (\Exception $e) {
  118.                 $exceptions[] = $e;
  119.                 unset($customFields[$key]);
  120.             }
  121.         }
  122.         foreach ($exceptions as $exception) {
  123.             $this->helper->error($exception->getMessage(), $exception);
  124.         }
  125.         try {
  126.             $order->setCustomFields($customFields);
  127.             $this->orderRepository->update([
  128.                 [
  129.                     'id' => $order->getId(),
  130.                     'customFields' => $customFields
  131.                 ]
  132.             ], $context);
  133.         } catch (\Exception $e) {
  134.             // Custom fields could not be saved
  135.             $this->helper->error($e->getMessage(), $e);
  136.         }
  137.     }
  138. }