custom/plugins/CoeWishlistSw6/src/Core/Content/Product/Subscriber/ProductLoadedSubscriber.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeWishlistSw6\Core\Content\Product\Subscriber;
  3. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
  4. use CoeWishlistSw6\Core\Content\Wishlist\WishlistCollection;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ProductLoadedSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var WishlistServiceInterface
  14.      */
  15.     private $wishlistService;
  16.     /**
  17.      * CustomerLoadedSubscriber constructor.
  18.      * @param WishlistServiceInterface $wishlistService
  19.      */
  20.     public function __construct(
  21.         WishlistServiceInterface $wishlistService
  22.     )
  23.     {
  24.         $this->wishlistService $wishlistService;
  25.     }
  26.     /**
  27.      * @return array
  28.      * @author Jeffry Block <[email protected]>
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.            'sales_channel.product.loaded' => 'onProductLoaded'
  34.         ];
  35.     }
  36.     /**
  37.      * @param EntityLoadedEvent $event
  38.      * @author Jeffry Block <[email protected]>
  39.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  40.      */
  41.     public function onProductLoaded(SalesChannelEntityLoadedEvent $event): void
  42.     {
  43.         /** @var WishlistCollection $lists */
  44.         $lists $this->wishlistService->loadLists($event->getSalesChannelContext(), ["notes"]);
  45.         /** @var SalesChannelProductEntity $salesChannelProduct */
  46.         foreach($event->getEntities() as $salesChannelProduct){
  47.             $isInUserWishlist $this->wishlistService->userHasSpecificProductInAnyList($salesChannelProduct->getId(), $event->getSalesChannelContext(), $lists);
  48.             $salesChannelProduct->addExtension("wishlist", new ArrayEntity(["isInUserWishlist" => $isInUserWishlist]));
  49.         }
  50.     }
  51. }