src/StoreFront/Subscriber/ProductExtensionSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\StoreFront\Subscriber;
  4. use Bodymed\Webshop\Constants;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\ProductDefinition;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  17. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  18. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class ProductExtensionSubscriber implements EventSubscriberInterface
  21. {
  22.     private EntityRepositoryInterface $productRepository;
  23.     private SalesChannelRepositoryInterface $salesChannelProductRepository;
  24.     public function __construct(
  25.         EntityRepositoryInterface $productRepository,
  26.         SalesChannelRepositoryInterface $salesChannelProductRepository
  27.     )
  28.     {
  29.         $this->productRepository $productRepository;
  30.         $this->salesChannelProductRepository $salesChannelProductRepository;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  36.             ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  37.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  38.             //ProductListingResultEvent::class => 'onProductListingResult',
  39.             #'sales_channel.product.loaded' => 'salesChannelProductLoaded',
  40.             ProductEvents::PRODUCT_LOADED_EVENT => 'addParentCover',
  41.             ProductSearchCriteriaEvent::class => 'onProductListingCriteria',
  42.             //ProductSearchResultEvent::class => 'onProductListingResult',
  43.         ];
  44.     }
  45.     public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
  46.     {
  47.         $event->getCriteria()->addAssociation('tags');
  48.     }
  49.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  50.     {
  51.         $event->getCriteria()->addAssociation('tags');
  52.         $event->getCriteria()->addAssociation('category');
  53.     }
  54.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  55.     {
  56.         $product $event->getPage()->getProduct();
  57.         $context $event->getContext();
  58.         $salesChannelContext $event->getSalesChannelContext();
  59.         if (isset($product->getCustomFields()[Constants::PRODUCT_CUSTOM_PRODUCT_CONFIGURATION_WEBINAR_PRODUCT_KEY])) {
  60.             $webinarProductId $product->getCustomFields()[Constants::PRODUCT_CUSTOM_PRODUCT_CONFIGURATION_WEBINAR_PRODUCT_KEY];
  61.             if (Uuid::isValid($webinarProductId)) {
  62.                 $webinarProduct $this->salesChannelProductRepository
  63.                                         ->search(new Criteria([$webinarProductId]), $salesChannelContext)
  64.                                         ->first()
  65.                 ;
  66.                 if ($webinarProduct instanceof SalesChannelProductEntity) {
  67.                     $product->addExtension(Constants::PRODUCT_WEBINAR_PRODUCT_EXTENSION_KEY$webinarProduct);
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     public function onProductListingResult(ProductListingResultEvent $event): void
  73.     {
  74.     }
  75.     public function addParentCover(EntityLoadedEvent $event): void
  76.     {
  77.         $context $event->getContext();
  78.         if ($event->getDefinition() instanceof ProductDefinition) {
  79.             /* @var $product ProductEntity */
  80.             foreach ($event->getEntities() as $product) {
  81.                 if ($product->getParentId() !== null) {
  82.                     $criteria = (new Criteria([$product->getParentId()]))
  83.                         ->addAssociation('cover')
  84.                     ;
  85.                     $parent $this->productRepository->search($criteria$context)->first();
  86.                     if ($parent) {
  87.                         $product->addExtension(Constants::PARENT_COVER$parent->getCover());
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }