custom/static-plugins/EfbStorefront/Subscriber/ProductDetailSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\EFB\Storefront\Subscriber;
  3. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaCollection;
  4. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  7. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  8. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  9. use Shopware\Core\Content\Property\PropertyGroupCollection;
  10. use Shopware\Core\Content\Property\PropertyGroupEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  18. use Shopware\Core\Framework\Struct\Struct;
  19. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  20. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  21. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  22. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  23. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  24. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Uandi\EFB\Storefront\Service\VariantLoader;
  27. use Uandi\EFB\Storefront\Struct\ProductVariantTableStruct;
  28. class ProductDetailSubscriber implements EventSubscriberInterface
  29. {
  30.     const BADGE_TAG_NAME 'badge';
  31.     protected SalesChannelRepositoryInterface $productRepository;
  32.     protected VariantLoader $variantLoader;
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  37.             ProductPageCriteriaEvent::class => 'extendProductPageCriteria'
  38.         ];
  39.     }
  40.     public function __construct(SalesChannelRepositoryInterface $repositoryVariantLoader $variantLoader)
  41.     {
  42.         $this->productRepository $repository;
  43.         $this->variantLoader $variantLoader;
  44.     }
  45.     public function extendProductPageCriteria(ProductPageCriteriaEvent $event)
  46.     {
  47.         $event->getCriteria()->addAssociation('media.media.tags');
  48.     }
  49.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  50.     {
  51.         $product $event->getPage()->getProduct();
  52.         $context $event->getSalesChannelContext();
  53.         $this->loadParent($product$context);
  54.         $this->filterBadges($product);
  55.         $this->variantLoader->loadVariantTable($product$context);
  56.     }
  57.     private function loadParent(ProductEntity $productSalesChannelContext $context): void
  58.     {
  59.         if ($product->getParentId()) {
  60.             $parent $this->productRepository->search(new Criteria([$product->getParentId()]), $context)->first();
  61.             if ($parent !== null) {
  62.                 $product->setParent($parent);
  63.             }
  64.         }
  65.     }
  66.     private function filterBadges(ProductEntity $product): void
  67.     {
  68.         $mediaCollection $product->getMedia();
  69.         $badges $mediaCollection->filter(static function(ProductMediaEntity $media) {
  70.             $tags $media->getMedia()->getTags();
  71.             return $tags && $tags->filterByProperty('name', static::BADGE_TAG_NAME)->count();
  72.         });
  73.         foreach ($badges->getIds() as $mediaId) {
  74.             $mediaCollection->remove($mediaId);
  75.         }
  76.         $product->addExtension('badges'$badges);
  77.     }
  78. }