<?php
namespace Wab\Wishlist\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageSubscriber implements EventSubscriberInterface
{
protected $wishlistRepository;
public function __construct(EntityRepositoryInterface $wishlistRepository)
{
$this->wishlistRepository = $wishlistRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductsLoaded',
PageLoadedEvent::class => 'onPageLoaded'
];
}
public function onProductsLoaded(ProductPageLoadedEvent $event)
{
$customer = $event->getSalesChannelContext()->getCustomer();
$productId = $event->getPage()->getProduct()->getId();
if ($customer == null) {
$event->getPage()->addExtension('on_wishlist', new OnWishlistStruct(false));
} else {
$customerId = $customer->getId();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('customerId',$customerId));
$criteria->addFilter(new EqualsFilter('productId',$productId));
$entry = $this->wishlistRepository->search($criteria,$event->getContext())->first();
if($entry == null){
$event->getPage()->addExtension('on_wishlist', new OnWishlistStruct(false));
}else{
$event->getPage()->addExtension('on_wishlist', new OnWishlistStruct(true));
}
}
}
public function onPageLoaded(PageLoadedEvent $event)
{
$event->getPage()->getMetaInformation()->setMetaTitle('asd');
}
}