custom/plugins/LoyxxSeminar/src/Subscriber/OrderLoadedEventSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace LoyxxSeminar\Subscriber;
  3. use LoyxxSeminar\Core\Checkout\Order\OrderParticipant\OrderParticipantCollection;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  7. use Shopware\Core\Checkout\Order\OrderEntity;
  8. use Shopware\Core\Checkout\Order\OrderEvents;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class OrderLoadedEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var EntityRepositoryInterface
  18.      */
  19.     protected $orderParticipantsRepository;
  20.     /**
  21.      * StorefrontRenderEventSubscriber constructor.
  22.      * @param  EntityRepositoryInterface  $orderParticipantsRepository
  23.      */
  24.     public function __construct(EntityRepositoryInterface $orderParticipantsRepository)
  25.     {
  26.         $this->orderParticipantsRepository $orderParticipantsRepository;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             OrderEvents::ORDER_LOADED_EVENT => "addParticipantsToOderLineItems",
  32.         ];
  33.     }
  34.     public function addParticipantsToOderLineItems(EntityLoadedEvent $event)
  35.     {
  36.         // Load participants to current order line items
  37.         // Take all line item keys and the search participants for this keys
  38.         // Add participant to line item by filtering by line item key through the result
  39.         $lineItems = [];
  40.         foreach ($event->getEntities() as $order) {
  41.             /**@var OrderEntity $order */
  42.             if ($order->getLineItems() === null) {
  43.                 continue;
  44.             }
  45. //            $lineItems += $this->filterByType($order->getLineItems(),LineItem::PRODUCT_LINE_ITEM_TYPE)->getElements();
  46.             $lineItems += $order->getLineItems()->filterByType(LineItem::PRODUCT_LINE_ITEM_TYPE)->getElements();
  47.         }
  48.         if (count($lineItems) === 0) {
  49.             return;
  50.         }
  51.         $lineItemsCollection = new OrderLineItemCollection($lineItems);
  52.         $lineItemsKeys $lineItemsCollection->getKeys();
  53.         $criteria = (new Criteria())
  54.             ->addFilter(new EqualsAnyFilter('orderLineItemId'$lineItemsKeys));
  55.         /** @var OrderParticipantCollection $participantsCollection */
  56.         $participantsCollection $this->orderParticipantsRepository->search(
  57.             $criteria,
  58.             $event->getContext()
  59.         )->getEntities();
  60.         foreach ($lineItemsCollection as $lineItem) {
  61.             /** @var OrderLineItemEntity $lineItem */
  62.             $participants $participantsCollection->filterByProperty('orderLineItemId'$lineItem->getId());
  63.             if ($participants->count()) {
  64.                 $lineItem->addExtension('participants'$participants);
  65.             }
  66.         }
  67.     }
  68.     private function filterByType($elements$type)
  69.     {
  70.         return $elements->filter(function (OrderLineItemEntity $lineItem) use ($type) {
  71.             return $lineItem->getType() === $type;
  72.         });
  73.     }
  74. }