custom/plugins/WbfkProductDownloads/src/Subscriber/ProductSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\ProductDownloads\Subscriber;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Struct\StructCollection;
  10. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  11. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  12. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var EntityRepository
  18.      */
  19.     private EntityRepository $productDownloadRepository;
  20.     public function __construct(EntityRepository $productDownloadRepository)
  21.     {
  22.         $this->productDownloadRepository $productDownloadRepository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProductPageCriteriaEvent::class => 'onProductCriteriaLoaded',
  28.             ProductPageLoadedEvent::class => 'loadProductPage',
  29.             MinimalQuickViewPageLoadedEvent::class => 'loadMinimalQuickViewPage',
  30.         ];
  31.     }
  32.     public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
  33.     {
  34.         $event->getCriteria()->addAssociation('wbfk_product_download');
  35.     }
  36.     public function loadProductPage(ProductPageLoadedEvent $event): void
  37.     {
  38.         // ToDo: This is a Fallback
  39.         // We can not set the product extension to "inherited".
  40.         // This would throw an error in the administration on saving a child product
  41.         // See: ProductExtension Definition
  42.         $product $event->getPage()->getProduct();
  43.         $this->addWbfkDownloadExtension($product$event->getContext());
  44.     }
  45.     public function loadMinimalQuickViewPage(MinimalQuickViewPageLoadedEvent $event): void
  46.     {
  47.         $product $event->getPage()->getProduct();
  48.         $this->addWbfkDownloadExtension($product$event->getContext());
  49.     }
  50.     private function addWbfkDownloadExtension(ProductEntity $productContext $context)
  51.     {
  52.         $productDownloads $product->getExtension('wbfk_product_download');
  53.         $productId $product->getParentId() ?? $product->getId();
  54.         if ($productId && (!$productDownloads || count($productDownloads->getElements()) === 0)) {
  55.             $criteria = new Criteria();
  56.             $criteria->addFilter(new EqualsFilter('productId'$productId));
  57.             $parentDownloads $this->productDownloadRepository->search($criteria$context)->getEntities();
  58.             if ($parentDownloads->count() > 0) {
  59.                 $product->addExtension('wbfk_product_download', new StructCollection($parentDownloads->getElements()));
  60.             }
  61.         }
  62.     }
  63. }