custom/plugins/NetiNextOrderFields/src/Subscriber/Checkout.php line 79

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextOrderFields\Subscriber;
  4. use NetInventors\NetiNextOrderFields\Core\Content\OrderFields\OrderFieldsEntity;
  5. use NetInventors\NetiNextOrderFields\Service\OrderAttributes;
  6. use NetInventors\NetiNextOrderFields\Service\PluginConfig;
  7. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. class Checkout implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var EntityRepositoryInterface
  22.      */
  23.     private $orderFieldRepository;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $orderFieldsOrderRepository;
  28.     /**
  29.      * @var EntityRepositoryInterface
  30.      */
  31.     private $orderFieldsOrderValueRepository;
  32.     /**
  33.      * @var RequestStack
  34.      */
  35.     private $requestStack;
  36.     /**
  37.      * @var OrderAttributes
  38.      */
  39.     private $orderAttributeService;
  40.     /**
  41.      * @var PluginConfig
  42.      */
  43.     private $pluginConfig;
  44.     public function __construct(
  45.         EntityRepositoryInterface $orderFieldRepository,
  46.         EntityRepositoryInterface $orderFieldsOrderRepository,
  47.         EntityRepositoryInterface $orderFieldsOrderValueRepository,
  48.         RequestStack $requestStack,
  49.         OrderAttributes $orderAttributeService,
  50.         PluginConfig $pluginConfig
  51.     ) {
  52.         $this->orderFieldRepository            $orderFieldRepository;
  53.         $this->orderFieldsOrderRepository      $orderFieldsOrderRepository;
  54.         $this->orderFieldsOrderValueRepository $orderFieldsOrderValueRepository;
  55.         $this->requestStack                    $requestStack;
  56.         $this->orderAttributeService           $orderAttributeService;
  57.         $this->pluginConfig                    $pluginConfig;
  58.     }
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmLoaded',
  63.             CheckoutOrderPlacedEvent::class       => 'onOrderPlaced',
  64.             MailBeforeValidateEvent::class        => 'beforeMailValidated',
  65.         ];
  66.     }
  67.     public function onConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  68.     {
  69.         if (!$this->pluginConfig->isActive()) {
  70.             return;
  71.         }
  72.         $salesChannelContext $event->getSalesChannelContext();
  73.         $salesChannelId      $salesChannelContext->getSalesChannel()->getId();
  74.         $criteria = (new Criteria())->addFilter(
  75.             new EqualsFilter('salesChannelId'$salesChannelId),
  76.             new EqualsFilter('active'1)
  77.         )
  78.             ->addAssociation('customerGroups')
  79.             ->addSorting(new FieldSorting('position'));
  80.         $orderFields $this->orderFieldRepository->search($criteria$event->getContext());
  81.         $event->getPage()->addExtension('neti_order_fields'$orderFields);
  82.     }
  83.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  84.     {
  85.         if (!$this->pluginConfig->isActive()) {
  86.             return;
  87.         }
  88.         $request   $this->requestStack->getCurrentRequest();
  89.         $parameter $request->request->get('orderField') ?? [];
  90.         $orderId            $event->getOrder()->getId();
  91.         $criteria           = new Criteria();
  92.         $criteria->addFilter(new EqualsFilter('active'true));
  93.         $criteria->addFilter(new EqualsFilter('salesChannelId'$event->getSalesChannelId()));
  94.         $orderFieldEntities $this->orderFieldRepository->search($criteria$event->getContext())->getElements();
  95.         $orderFields        = [];
  96.         $orderFieldsValues  = [];
  97.         /** @var OrderFieldsEntity $entity */
  98.         foreach ($orderFieldEntities as $id => $entity) {
  99.             $orderFields[$id]['label']          = $entity->getTranslation('label');
  100.             $orderFields[$id]['name']           = $entity->getName();
  101.             $orderFields[$id]['type']           = $entity->getType();
  102.             $orderFields[$id]['orderId']        = $orderId;
  103.             $orderFields[$id]['orderVersionId'] = $event->getOrder()->getVersionId();
  104.             $orderFields[$id]['orderFieldId']   = $entity->getId();
  105.             $orderFields[$id]['versionId']      = $event->getOrder()->getVersionId();
  106.             $orderFieldsValues[$id]['value'] = is_string($parameter[$id] ?? null)
  107.                 ? substr($parameter[$id], 0512)
  108.                 : null;
  109.             foreach ($entity->getExtension('netiOrderFieldsVal')->getElements() as $value) {
  110.                 if ($value->getValue() === $orderFieldsValues[$id]['value']) {
  111.                     $orderFieldsValues[$id]['valueLabel'] = $value->getTranslated()['label'];
  112.                     break;
  113.                 }
  114.             }
  115.         }
  116.         if ($orderFields === []) {
  117.             return;
  118.         }
  119.         $orderFields       array_intersect_key($orderFields$orderFieldsValues);
  120.         $context           $event->getContext();
  121.         $result            $this->orderFieldsOrderRepository->create(array_values($orderFields), $context);
  122.         /** @var EntityWrittenEvent|null $event */
  123.         $entityEvent $result->getEvents()->first();
  124.         $entityIds   = [];
  125.         if ($entityEvent instanceof EntityWrittenEvent) {
  126.             foreach ($entityEvent->getIds() as $id) {
  127.                 $entityIds[] = $id;
  128.             }
  129.         }
  130.         $criteria             = new Criteria($entityIds);
  131.         $orderFieldAttributes $this->orderFieldsOrderRepository->search($criteria$context)->getElements();
  132.         foreach ($orderFieldAttributes as $attribute) {
  133.             $orderFieldsValues[$attribute->getOrderFieldId()]['orderAttributeId'] = $attribute->getId();
  134.             $orderFieldsValues[$attribute->getOrderFieldId()]['orderAttributeVersionId'] = $event->getOrder()->getVersionId();
  135.         }
  136.         $this->orderFieldsOrderValueRepository->create(array_values($orderFieldsValues), $context);
  137.     }
  138.     public function beforeMailValidated(MailBeforeValidateEvent $event)
  139.     {
  140.         if (!$this->pluginConfig->isActive()) {
  141.             return;
  142.         }
  143.         $templateData $event->getTemplateData();
  144.         if (!(isset($templateData['order']) && $templateData['order'] instanceof OrderEntity)) {
  145.             return;
  146.         }
  147.         $order   $templateData['order'];
  148.         $orderId $order->getId();
  149.         $criteria = (new Criteria())->addFilter(new EqualsFilter('orderId'$orderId));
  150.         $context  $event->getContext();
  151.         $orderFields $this->orderFieldsOrderRepository->search(
  152.             $criteria,
  153.             $context
  154.         )->getEntities();
  155.         if ([] === $orderFields->getElements()) {
  156.             return;
  157.         }
  158.         $order->addExtension('netiOrderFieldsOrderAttribute'$orderFields);
  159.         $this->orderAttributeService->getAttributeValues([ $order->getId() => $order ], $context);
  160.     }
  161. }