<?php declare(strict_types=1);
namespace Nd\Stl\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Struct\Struct;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddLookToProductPageData implements EventSubscriberInterface
{
private EntityRepositoryInterface $lookProductRepository;
public function __construct(EntityRepositoryInterface $lookProductRepository)
{
$this->lookProductRepository = $lookProductRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'addLookToProductPage'
];
}
public function addLookToProductPage(ProductPageLoadedEvent $event): void
{
$productPage = $event->getPage();
$product = $productPage->getProduct();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('productId', $product->getId()));
$criteria->getAssociation("look");
$criteria->getAssociation("look.cover");
$criteria->getAssociation("look.fashionGuideAvatarMedia");
$lookProducts = $this->lookProductRepository->search($criteria, $event->getContext());
if($lookProducts->count() >= 1){
$productPage->addExtension('look', $lookProducts->first()->getLook());
}
}
}