custom/static-plugins/SamsonCustomer/src/Subscriber/ProductPageSubscriber.php line 40

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\Product\Aggregate\ProductReview\ProductReviewEntity;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ProductPageSubscriber implements EventSubscriberInterface
  20. {
  21.     private EntityRepositoryInterface $mediaRepository;
  22.     public function __construct(EntityRepositoryInterface $mediaRepository)
  23.     {
  24.         $this->mediaRepository $mediaRepository;
  25.     }
  26.     /** {@inheritDoc} */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  31.         ];
  32.     }
  33.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  34.     {
  35.         $this->setReviewPointsAndCount($event);
  36.         $this->setProductBackgroundImage($event);
  37.     }
  38.     private function setReviewPointsAndCount(ProductPageLoadedEvent $event): void
  39.     {
  40.         if (!is_null($event->getPage()->getProduct()->getCmsPage())) {
  41.             return;
  42.         }
  43.         $reviews $event->getPage()->getReviews();
  44.         $points 0;
  45.         if ($reviews->count() > 0) {
  46.             /** @var ProductReviewEntity $review */
  47.             foreach ($reviews as $review) {
  48.                 $points += $review->getPoints();
  49.             }
  50.             $points round($points $reviews->count() * 2) / 2;
  51.         }
  52.         $event->getPage()->getProduct()->assign(['reviewPoints' => $points]);
  53.         $event->getPage()->getProduct()->assign(['reviewCount' => $reviews->count()]);
  54.     }
  55.     private function setProductBackgroundImage(ProductPageLoadedEvent $event): void
  56.     {
  57.         $product $event->getPage()->getProduct();
  58.         if (!isset($product->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_SHOW_BACKGROUND_IMAGE])) {
  59.             return;
  60.         }
  61.         $showBackgroundImage $product->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_SHOW_BACKGROUND_IMAGE];
  62.         if ($showBackgroundImage) {
  63.             if (!isset($product->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_BACKGROUND_IMAGE])) {
  64.                 return;
  65.             }
  66.             $mediaId $product->getCustomFields()[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_BACKGROUND_IMAGE];
  67.             $media $this->mediaRepository->search(new Criteria([$mediaId]), $event->getContext())->first();
  68.             if (!is_null($media)) {
  69.                 $customFields $product->getCustomFields();
  70.                 $customFields[ProductCustomFieldConstants::CUSTOM_FIELD_PRODUCT_IMAGE_BACKGROUND_IMAGE] = $media;
  71.                 $product->setCustomFields($customFields);
  72.             }
  73.         }
  74.     }
  75. }