<?php declare(strict_types=1);
namespace MoorlProductAccessories\Subscriber;
use MoorlProductAccessories\Core\Service\ProductAccessoriesService;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class StorefrontSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var ProductAccessoriesService
*/
private $productAccessoriesService;
public function __construct(
SystemConfigService $systemConfigService,
ProductAccessoriesService $productAccessoriesService
)
{
$this->systemConfigService = $systemConfigService;
$this->productAccessoriesService = $productAccessoriesService;
}
public static function getSubscribedEvents(): array
{
return [
'sales_channel.moorl_pa_accessory.loaded' => 'loaded',
ProductPageCriteriaEvent::class => 'addProductPageCriteria',
ProductPageLoadedEvent::class => 'enrichProduct'
];
}
public function enrichProduct(ProductPageLoadedEvent $event): void
{
$this->productAccessoriesService->setSalesChannelContext($event->getSalesChannelContext());
$this->productAccessoriesService->enrichProductEntity($event->getPage()->getProduct());
}
public function addProductPageCriteria(ProductPageCriteriaEvent $event): void
{
$this->productAccessoriesService->enrichProductCriteria($event->getCriteria());
}
public function loaded(SalesChannelEntityLoadedEvent $event): void
{
$this->productAccessoriesService->setSalesChannelContext($event->getSalesChannelContext());
foreach ($event->getEntities() as $accessory) {
$this->productAccessoriesService->handlePrice($accessory);
}
}
}