custom/plugins/OkeonlineKejeDocumentImport/src/Storefront/Subscriber/OrderRouteRequestEventSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Okeonline\KejeDocumentImport\Storefront\Subscriber;
  3. use Shopware\Core\Checkout\Document\DocumentEntity;
  4. use Shopware\Core\Checkout\Document\DocumentService;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Event\RouteRequest\OrderRouteRequestEvent;
  11. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class OrderRouteRequestEventSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var OrderRouteRequestEvent
  17.      */
  18.     private $event;
  19.     /**
  20.      * @var EntityRepository
  21.      */
  22.     private $documentRepository;
  23.     /**
  24.      * @var SystemConfigService
  25.      */
  26.     private $configService;
  27.     /**
  28.      * @var DocumentService
  29.      */
  30.     private $documentService;
  31.     public function __construct(
  32.         EntityRepository $documentRepository,
  33.         SystemConfigService $configService,
  34.         DocumentService $documentService
  35.     )
  36.     {
  37.         $this->documentRepository $documentRepository;
  38.         $this->configService $configService;
  39.         $this->documentService $documentService;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44. //            OrderRouteRequestEvent::class => ['addDocumentsAssociation', 0],
  45.             AccountOrderPageLoadedEvent::class => ['addDocumentsToOrdersAlternative'0]
  46.         ];
  47.     }
  48.     public function addDocumentsAssociation(OrderRouteRequestEvent $event)
  49.     {
  50.         $this->event $event;
  51.         $criteria $event->getCriteria();
  52.         $criteria->addAssociation('documents');
  53.         // what is going wrong with this method is that Shopware somehow adds some filters to this association, thereby the documents are not showing...
  54.     }
  55.     public function addDocumentsToOrdersAlternative(AccountOrderPageLoadedEvent $event)
  56.     {
  57.         $orders $event->getPage()->getOrders();
  58.         $allOrderIds $orders->getIds();
  59.         $criteria = new Criteria();
  60.         $criteria->addAssociation('documentMediaFile');
  61.         $criteria->addFilter(new EqualsAnyFilter('orderId'$allOrderIds));
  62.         $criteria->addFilter(new EqualsAnyFilter('documentTypeId', [
  63.             $this->configService->get('OkeonlineKejeDocumentImport.config.invoiceDocumentType'),
  64.             $this->configService->get('OkeonlineKejeDocumentImport.config.deliveryNoteDocumentType')
  65.         ]));
  66.         $documentsResult $this->documentRepository->search($criteria$event->getContext());
  67.         foreach ($orders as $order)
  68.         {
  69.             $documentCollection $documentsResult->getEntities()->filterByProperty('orderId'$order->getId());
  70.             /**
  71.              * @var  string $key
  72.              * @var  DocumentEntity $document
  73.              */
  74.             foreach ($documentCollection as $key => $document)
  75.             {
  76.                 try {
  77.                     $this->documentService->getDocument($document$event->getContext());
  78.                 } catch (\Throwable $e) {
  79.                     $documentCollection->remove($key);
  80.                 }
  81.             }
  82.             /** @var OrderEntity $order */
  83.             $order->setDocuments($documentCollection);
  84.         }
  85.     }
  86. }