<?php declare(strict_types=1);
namespace CoeWishlistSw6\Core\Content\Product\Subscriber;
use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
use CoeWishlistSw6\Core\Content\Wishlist\WishlistCollection;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var WishlistServiceInterface
*/
private $wishlistService;
/**
* CustomerLoadedSubscriber constructor.
* @param WishlistServiceInterface $wishlistService
*/
public function __construct(
WishlistServiceInterface $wishlistService
)
{
$this->wishlistService = $wishlistService;
}
/**
* @return array
* @author Jeffry Block <[email protected]>
*/
public static function getSubscribedEvents(): array
{
return [
'sales_channel.product.loaded' => 'onProductLoaded'
];
}
/**
* @param EntityLoadedEvent $event
* @author Jeffry Block <[email protected]>
* @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
*/
public function onProductLoaded(SalesChannelEntityLoadedEvent $event): void
{
/** @var WishlistCollection $lists */
$lists = $this->wishlistService->loadLists($event->getSalesChannelContext(), ["notes"]);
/** @var SalesChannelProductEntity $salesChannelProduct */
foreach($event->getEntities() as $salesChannelProduct){
$isInUserWishlist = $this->wishlistService->userHasSpecificProductInAnyList($salesChannelProduct->getId(), $event->getSalesChannelContext(), $lists);
$salesChannelProduct->addExtension("wishlist", new ArrayEntity(["isInUserWishlist" => $isInUserWishlist]));
}
}
}