vendor/crehler/arrangements/src/Subscriber/ProductListingResultSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   27 lip 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Arrangements\Subscriber;
  13. use Crehler\Arrangements\Entity\Arrangement\ArrangementEntity;
  14. use Crehler\Arrangements\Service\ArrangementService;
  15. use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
  16. use Monolog\Logger;
  17. use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean;
  18. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  19. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  20. use Shopware\Core\Content\Product\ProductEvents;
  21. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  22. use Shopware\Core\Framework\Context;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  24. use Shopware\Core\Framework\Struct\ArrayStruct;
  25. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. class ProductListingResultSubscriber implements EventSubscriberInterface
  28. {
  29.     public function __construct(
  30.         private readonly ArrangementService $arrangementService,
  31.         private readonly ProductStatusStorefrontService $productStatusService,
  32.         private bool $skip false
  33.     )
  34.     {
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             ProductEvents::PRODUCT_LISTING_RESULT => 'handleListingLoaded',
  40.             ProductPageLoadedEvent::class => 'handleProductLoaded',
  41.             'arrangement_product.loaded' => 'handleProductArrangementLoaded',
  42.             ProductIndexerEvent::class =>['onProductIndexer'1],
  43.         ];
  44.     }
  45.     public function handleProductArrangementLoaded(EntityLoadedEvent $event): void
  46.     {
  47.         foreach ($event->getEntities() as $arrangementsProduct) {
  48.             $product $arrangementsProduct->getProduct();
  49.             $product->addExtension('productStatus'$this->productStatusService->getProductStatus($product$event->getContext()));
  50.         }
  51.     }
  52.     public function onProductIndexer($event)
  53.     {
  54.         $this->skip true;
  55.     }
  56.     public function handleProductLoaded(ProductPageLoadedEvent $event): void
  57.     {
  58.         if ($event->getContext()->getScope() === Context::CRUD_API_SCOPE ||
  59.             $event->getContext()->getScope() === Context::SYSTEM_SCOPE ||
  60.             $this->skip
  61.         ) {
  62.             return;
  63.         }
  64.         /** @var ArrayStruct $foreignKeys */
  65.         $foreignKeys $event->getPage()->getProduct()->getExtension('foreignKeys');
  66.         if (null !== $foreignKeys->offsetGet('arrangementId')) {
  67.             $arrangement $this->arrangementService->getArrangementById(
  68.                 $foreignKeys->offsetGet('arrangementId'),
  69.                 $event->getSalesChannelContext(),
  70.                 $event->getRequest()
  71.             );
  72.             $event->getPage()->getProduct()->addExtension('arrangement'$arrangement);
  73.             /** @var ArrangementEntity $arrangement */
  74.             $arrangement $event->getPage()->getProduct()->getExtension('arrangement');
  75.             $arrangement->setMainProduct($event->getPage()->getProduct());
  76.         }
  77.     }
  78.     public function handleListingLoaded(ProductListingResultEvent $event): void
  79.     {
  80.         if ($event->getContext()->getScope() === Context::CRUD_API_SCOPE) {
  81.             return;
  82.         }
  83.         /** @var SalesChannelProductEntity $product */
  84.         foreach ($event->getResult()->getElements() as $product) {
  85.             /** @var ArrayStruct $foreignKeys */
  86.             $foreignKeys $product->getExtension('foreignKeys');
  87.             if (null !== $foreignKeys->offsetGet('arrangementId')) {
  88.                 $arrangement $this->arrangementService->getArrangementById(
  89.                     $foreignKeys->offsetGet('arrangementId'),
  90.                     $event->getSalesChannelContext(),
  91.                     $event->getRequest()
  92.                 );
  93.                 $product->addExtension('arrangement'$arrangement);
  94.             }
  95.         }
  96.     }
  97. }