vendor/crehler/mojebambino-variants-advanced/src/Subscriber/ProductListingResultSubscriber.php line 59

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   12 lut 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\VariantsAdvanced\Subscriber;
  13. use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
  14. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  15. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  16. use Shopware\Core\Content\Property\PropertyEvents;
  17. use Shopware\Core\Content\Property\PropertyGroupEntity;
  18. use Shopware\Core\Framework\Context;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\System\CustomField\CustomFieldEvents;
  23. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  24. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  25. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  26. use Shopware\Storefront\Page\Product\Configurator\ProductPageConfiguratorLoader;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. class ProductListingResultSubscriber implements EventSubscriberInterface
  29. {
  30.     private ProductPageConfiguratorLoader $pageConfiguratorLoader;
  31.     private CrehlerContextService $crehlerContextService;
  32.     private SalesChannelRepositoryInterface $salesChannelProductRepository;
  33.     public function __construct(ProductPageConfiguratorLoader $pageConfiguratorLoaderCrehlerContextService $crehlerContextServiceSalesChannelRepositoryInterface $salesChannelProductRepository)
  34.     {
  35.         $this->pageConfiguratorLoader $pageConfiguratorLoader;
  36.         $this->crehlerContextService $crehlerContextService;
  37.         $this->salesChannelProductRepository $salesChannelProductRepository;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             'sales_channel.product.loaded' => 'handleProductResult',
  43.         ];
  44.     }
  45.     public function handleProductResult(SalesChannelEntityLoadedEvent $event): void
  46.     {
  47.         if ($this->crehlerContextService->get(CrehlerContextService::SITEMAP_GENERATE) || $this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATE)) return;
  48.         $products $event->getEntities();
  49.         $this->addExtensionToProducts($products$event->getSalesChannelContext());
  50.     }
  51.     private function addExtensionToProducts(array $productsSalesChannelContext $salesChannelContext): void
  52.     {
  53.         /** @var SalesChannelProductEntity $product */
  54.         foreach ($products as $product) {
  55.             if ($product->getParentId() === null && $product->getMainVariantId()) {
  56.                 $productVariant $this->salesChannelProductRepository->search(new Criteria([$product->getMainVariantId()]), $salesChannelContext)->first();
  57.                 $product->addExtensions(
  58.                     [
  59.                         'configuratorSettings' => $this->pageConfiguratorLoader->load($productVariant$salesChannelContext),
  60.                         'mainVariantProduct'=> $productVariant,
  61.                     ]
  62.                 );
  63.             } else {
  64.                 $product->addExtensions(
  65.                     [
  66.                         'configuratorSettings' => $this->pageConfiguratorLoader->load($product$salesChannelContext),
  67.                     ]
  68.                 );
  69.             }
  70.         }
  71.     }
  72. }