custom/plugins/GrimmTheme/src/Storefront/Subscriber/ProductDetailSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class ProductDetailSubscriber implements EventSubscriberInterface
  6. {
  7.     private static array $mimeTypeFilter = [
  8.         'application/pdf'
  9.     ];
  10.     
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             ProductPageLoadedEvent::class => 'onProductDetailPage'
  15.         ];
  16.     }
  17.     /**
  18.      * Remove pdf files from product detail page
  19.      *
  20.      * @param ProductPageLoadedEvent $productPageLoadedEvent
  21.      */
  22.     public function onProductDetailPage(ProductPageLoadedEvent $productPageLoadedEvent)
  23.     {
  24.        $product $productPageLoadedEvent->getPage()->getProduct();
  25.        $mediaCollection $product->getMedia();
  26.        $downloads = [];
  27.        foreach ($mediaCollection as $key => $mediaEntity) {
  28.             if(in_array($mediaEntity->getMedia()->getMimeType(), self::$mimeTypeFilter)) {
  29.                 $mediaCollection->remove($key);
  30.                 $downloads[] = $mediaEntity;
  31.             }
  32.        }
  33.       if($mediaCollection) {
  34.          $product->setMedia($mediaCollection);
  35.       }
  36.       if(!empty($downloads)) {
  37.           $customFields $product->getCustomFields();
  38.           $customFields['downloads'] = $downloads;
  39.           $product->setCustomFields($customFields);
  40.       }
  41.     }
  42. }