custom/plugins/WabWishlist/src/Subscriber/ProductPageSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace Wab\Wishlist\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Storefront\Page\PageLoadedEvent;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ProductPageSubscriber implements EventSubscriberInterface
  10. {
  11.     protected $wishlistRepository;
  12.     public function __construct(EntityRepositoryInterface $wishlistRepository)
  13.     {
  14.         $this->wishlistRepository $wishlistRepository;
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  20.             PageLoadedEvent::class => 'onPageLoaded'
  21.         ];
  22.     }
  23.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  24.     {
  25.         $customer $event->getSalesChannelContext()->getCustomer();
  26.         $productId $event->getPage()->getProduct()->getId();
  27.         if ($customer == null) {
  28.             $event->getPage()->addExtension('on_wishlist', new OnWishlistStruct(false));
  29.         } else {
  30.             $customerId $customer->getId();
  31.             $criteria = new Criteria();
  32.             $criteria->addFilter(new EqualsFilter('customerId',$customerId));
  33.             $criteria->addFilter(new EqualsFilter('productId',$productId));
  34.             $entry $this->wishlistRepository->search($criteria,$event->getContext())->first();
  35.             if($entry == null){
  36.                 $event->getPage()->addExtension('on_wishlist', new OnWishlistStruct(false));
  37.             }else{
  38.                 $event->getPage()->addExtension('on_wishlist', new OnWishlistStruct(true));
  39.             }
  40.         }
  41.     }
  42.     public function onPageLoaded(PageLoadedEvent $event)
  43.     {
  44.         $event->getPage()->getMetaInformation()->setMetaTitle('asd');
  45.     }
  46. }