custom/static-plugins/SamsonCustomer/src/Subscriber/ProductListingResultSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2022
  11.  *
  12.  ***/
  13. use Samson\CustomFieldSet\Constants\ProductCustomFieldConstants;
  14. use Shopware\Core\Content\Media\MediaEntity;
  15. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  16. use Shopware\Core\Content\Product\ProductEvents;
  17. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  18. use Shopware\Core\Framework\Context;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class ProductListingResultSubscriber implements EventSubscriberInterface
  23. {
  24.     private EntityRepositoryInterface $mediaRepository;
  25.     public function __construct(EntityRepositoryInterface $mediaRepository)
  26.     {
  27.         $this->mediaRepository $mediaRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             ProductEvents::PRODUCT_LISTING_RESULT  => 'onProductListingResult'
  33.         ];
  34.     }
  35.     public function onProductListingResult(ProductListingResultEvent $event)
  36.     {
  37.         $context $event->getContext();
  38.         $event->getResult()->map(function (SalesChannelProductEntity $product) use ($context) {
  39.             // todo: fetch all media ONCE
  40.             $this->setProductBackgroundImage($product$context);
  41.         });
  42.     }
  43.     private function setProductBackgroundImage(SalesChannelProductEntity $salesChannelProductEntityContext $context): void
  44.     {
  45.         if (!isset($salesChannelProductEntity->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_SHOW_BACKGROUND_IMAGE])) {
  46.             return;
  47.         }
  48.         $showBackgroundImage $salesChannelProductEntity->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_SHOW_BACKGROUND_IMAGE];
  49.         if ($showBackgroundImage) {
  50.             if (!isset($salesChannelProductEntity->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_BACKGROUND_IMAGE])) {
  51.                 return;
  52.             }
  53.             $mediaId $salesChannelProductEntity->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_BACKGROUND_IMAGE];
  54.             if ($mediaId instanceof MediaEntity) {
  55.                 return;
  56.             }
  57.             $media $this->mediaRepository->search(new Criteria([$mediaId]), $context)->first();
  58.             if (!is_null($media)) {
  59.                 $customFields $salesChannelProductEntity->getCustomFields();
  60.                 $customFields[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_BACKGROUND_IMAGE] = $media;
  61.                 $salesChannelProductEntity->setCustomFields($customFields);
  62.             }
  63.         }
  64.     }
  65. }