<?php
declare(strict_types=1);
namespace Bodymed\Webshop\StoreFront\Subscriber;
use Bodymed\Webshop\Constants;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductExtensionSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productRepository;
private SalesChannelRepositoryInterface $salesChannelProductRepository;
public function __construct(
EntityRepositoryInterface $productRepository,
SalesChannelRepositoryInterface $salesChannelProductRepository
)
{
$this->productRepository = $productRepository;
$this->salesChannelProductRepository = $salesChannelProductRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => 'onProductListingCriteria',
ProductPageCriteriaEvent::class => 'onProductPageCriteria',
ProductPageLoadedEvent::class => 'onProductPageLoaded',
//ProductListingResultEvent::class => 'onProductListingResult',
#'sales_channel.product.loaded' => 'salesChannelProductLoaded',
ProductEvents::PRODUCT_LOADED_EVENT => 'addParentCover',
ProductSearchCriteriaEvent::class => 'onProductListingCriteria',
//ProductSearchResultEvent::class => 'onProductListingResult',
];
}
public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('tags');
}
public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('tags');
$event->getCriteria()->addAssociation('category');
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$product = $event->getPage()->getProduct();
$context = $event->getContext();
$salesChannelContext = $event->getSalesChannelContext();
if (isset($product->getCustomFields()[Constants::PRODUCT_CUSTOM_PRODUCT_CONFIGURATION_WEBINAR_PRODUCT_KEY])) {
$webinarProductId = $product->getCustomFields()[Constants::PRODUCT_CUSTOM_PRODUCT_CONFIGURATION_WEBINAR_PRODUCT_KEY];
if (Uuid::isValid($webinarProductId)) {
$webinarProduct = $this->salesChannelProductRepository
->search(new Criteria([$webinarProductId]), $salesChannelContext)
->first()
;
if ($webinarProduct instanceof SalesChannelProductEntity) {
$product->addExtension(Constants::PRODUCT_WEBINAR_PRODUCT_EXTENSION_KEY, $webinarProduct);
}
}
}
}
public function onProductListingResult(ProductListingResultEvent $event): void
{
}
public function addParentCover(EntityLoadedEvent $event): void
{
$context = $event->getContext();
if ($event->getDefinition() instanceof ProductDefinition) {
/* @var $product ProductEntity */
foreach ($event->getEntities() as $product) {
if ($product->getParentId() !== null) {
$criteria = (new Criteria([$product->getParentId()]))
->addAssociation('cover')
;
$parent = $this->productRepository->search($criteria, $context)->first();
if ($parent) {
$product->addExtension(Constants::PARENT_COVER, $parent->getCover());
}
}
}
}
}
}