custom/plugins/CioPodProducts/src/Subscriber/BusinessEventCollectorSubscriber.php line 171

Open in your IDE?
  1. <?php
  2. namespace CioPodProducts\Subscriber;
  3. use CioPodProducts\CioPodProducts;
  4. use CioPodProducts\Error\PodMixedCartError;
  5. use CioPodProducts\Event\PodCustomerApprovalEvent;
  6. use CioPodProducts\Event\PodFinalApprovalEvent;
  7. use CioPodProducts\Event\PodOrderCreatedEvent;
  8. use CioPodProducts\Event\PodPhotographerGalleryUploadedEvent;
  9. use CioPodProducts\Event\PodSupplierPreviewUploadedEvent;
  10. use CioPodProducts\Helper\PodOrdersAssociationsHelper;
  11. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  12. use Shopware\Core\Checkout\Order\OrderEntity;
  13. use Shopware\Core\Content\Product\ProductEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  18. use Shopware\Core\Framework\Uuid\Uuid;
  19. use Shopware\Storefront\Event\StorefrontRenderEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Shopware\Core\Framework\Event\BusinessEventCollector;
  24. class BusinessEventCollectorSubscriber implements EventSubscriberInterface
  25. {
  26.     protected BusinessEventCollector $businessEventCollector;
  27.     protected EventDispatcherInterface $eventDispatcher;
  28.     protected EntityRepository $entityRepository;
  29.     protected EntityRepository $orderRepository;
  30.     public function __construct(BusinessEventCollector $businessEventCollectorEventDispatcherInterface $eventDispatcherEntityRepository $productRepositoryEntityRepository $orderRepository)
  31.     {
  32.         $this->businessEventCollector $businessEventCollector;
  33.         $this->eventDispatcher $eventDispatcher;
  34.         $this->entityRepository $productRepository;
  35.         $this->orderRepository $orderRepository;
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             BusinessEventCollectorEvent::NAME => 'onBusinessEventCollectorEvent',
  41.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent',
  42.             StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
  43.         ];
  44.     }
  45.     public function onBusinessEventCollectorEvent(BusinessEventCollectorEvent $event)
  46.     {
  47.         $collection $event->getCollection();
  48.         // add event PodOrderCreatedEvent to the collection
  49.         $definition $this->businessEventCollector->define(PodOrderCreatedEvent::class);
  50.         if (!$definition) {
  51.             return;
  52.         }
  53.         $collection->set($definition->getName(), $definition);
  54.         // add event PodPhotographerGalleryUploadedEvent to the collection
  55.         $definition $this->businessEventCollector->define(PodPhotographerGalleryUploadedEvent::class);
  56.         if (!$definition) {
  57.             return;
  58.         }
  59.         $collection->set($definition->getName(), $definition);
  60.         // add event PodSupplierPreviewUploadedEvent to the collection
  61.         $definition $this->businessEventCollector->define(PodSupplierPreviewUploadedEvent::class);
  62.         if (!$definition) {
  63.             return;
  64.         }
  65.         $collection->set($definition->getName(), $definition);
  66.         // add event PodCustomerApprovalEvent to the collection
  67.         $definition $this->businessEventCollector->define(PodCustomerApprovalEvent::class);
  68.         if (!$definition) {
  69.             return;
  70.         }
  71.         $collection->set($definition->getName(), $definition);
  72.         // add event PodFinalApprovalEvent to the collection
  73.         $definition $this->businessEventCollector->define(PodFinalApprovalEvent::class);
  74.         if (!$definition) {
  75.             return;
  76.         }
  77.         $collection->set($definition->getName(), $definition);
  78.     }
  79.     public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event)
  80.     {
  81.         // pod orders are limited to a single line item
  82.         if ($event->getOrder()->getLineItems()->count() === 1) {
  83.             $lineItem $event->getOrder()->getLineItems()->first();
  84.             $criteria = new Criteria();
  85.             $criteria->addFilter(new EqualsFilter('id'$lineItem->getProductId()));
  86.             $product $this->entityRepository->search($criteria$event->getContext())->first();
  87.             if ($product instanceof ProductEntity) {
  88.                 $productCustomFields $product->getCustomFields();
  89.                 // check if the line item is a pod product
  90.                 if (is_array($productCustomFields) && array_key_exists(CioPodProducts::POD_PRODUCTS_CUSTOM_FIELD_IS_POD$productCustomFields) && $productCustomFields[CioPodProducts::POD_PRODUCTS_CUSTOM_FIELD_IS_POD]) {
  91.                     $lineItems $event->getOrder()->getLineItems();
  92.                     if ($lineItems && $lineItems->count() > 0) {
  93.                         $firstLineItem $lineItems->first();
  94.                         $payload $firstLineItem->getPayload();
  95.                         if (!isset($payload['customFields']) || !\is_array($payload['customFields'])) {
  96.                             $payload['customFields'] = [];
  97.                         }
  98.                         $this->orderRepository->update([
  99.                             [
  100.                                 'id' => $event->getOrder()->getId(),
  101.                                 'lineItems' => [
  102.                                     [
  103.                                         'id' => $firstLineItem->getId(),
  104.                                         'payload' => $payload,
  105.                                         'productId' => $firstLineItem->getProductId(),
  106.                                         'referencedId' => $firstLineItem->getReferencedId()
  107.                                     ]
  108.                                 ],
  109.                             ]
  110.                         ], $event->getContext());
  111.                     }
  112.                     $criteria = new Criteria();
  113.                     $criteria->addFilter(new EqualsFilter('id'$event->getOrder()->getId()));
  114.                     PodOrdersAssociationsHelper::addAssociationsToCriteria($criteria);
  115.                     $order $this->orderRepository->search($criteria$event->getContext())->first();
  116.                     // this event triggers the initial flow of the pod flows
  117.                     $podOrderCreatedEvent = new PodOrderCreatedEvent($event->getContext(), $order$event->getSalesChannelId());
  118.                     $this->eventDispatcher->dispatch($podOrderCreatedEvent);
  119.                 }
  120.             }
  121.         }
  122.     }
  123.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  124.     {
  125.         $parameters $event->getParameters();
  126.         if (!\is_array($parameters) || !array_key_exists('page'$parameters)) {
  127.             return;
  128.         }
  129.         $page $parameters['page'];
  130.         if (!$page instanceof CheckoutConfirmPage) {
  131.             return;
  132.         }
  133.         $lineItems $page->getCart()->getLineItems();
  134.         $hasPodProduct false;
  135.         $errors = [];
  136.         foreach ($lineItems as $lineItem) {
  137.             $payload $lineItem->getPayload();
  138.             if (array_key_exists('customFields'$payload) && is_array($payload['customFields']) && array_key_exists('custom_pod_products_isPodProduct'$payload['customFields']) && $payload['customFields']['custom_pod_products_isPodProduct'] === true) {
  139.                 $hasPodProduct true;
  140.                 break;
  141.             }
  142.         }
  143.         if ($lineItems->count() > && $hasPodProduct) {
  144.             $page->getCart()->addErrors(new PodMixedCartError());
  145.             $errors[] = ['type' => 'norm''message' => 'pod.checkout.noMixedBasketsError' ];
  146.             if (array_key_exists('errors'$event->getParameters())) {
  147.                 $event->setParameter('errors'array_merge($event->getParameters()['errors'], $errors));
  148.             } else {
  149.                 $event->setParameter('errors'$errors);
  150.             }
  151.         }
  152.     }
  153. }