<?php declare(strict_types=1);
namespace Huebert\AccessoriesDirectly\Subscriber;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductDetailSubscriber implements EventSubscriberInterface
{
protected $systemConfigService;
public function __construct(
SystemConfigService $systemConfigService
)
{
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => "onDetailPageLoaded",
];
}
public function onDetailPageLoaded(ProductPageLoadedEvent $pageLoadedEvent){
if(array_key_exists('HuebertAccessoriesDirectly', $this->systemConfigService->all($pageLoadedEvent->getSalesChannelContext()->getSalesChannel()->getId()))){
$config = $this->systemConfigService->all($pageLoadedEvent->getSalesChannelContext()->getSalesChannel()->getId())['HuebertAccessoriesDirectly']['config'];
//legacy code
$pageLoadedEvent->getPage()->assign(['acessoryOptions' => [
'alwaysShow' => !empty($config['alwaysShowAccessories']) ? true : false,
'accessoryPosition' => $config['accessoryPosition'],
'activateVariants' => $config['activateVariants'],
'showUnits' => $config['showUnits']
]
]);
//assign HuebertAccessoriesDirectly config
$pageLoadedEvent->getPage()->assign(['HuebertAccessoriesDirectly' => ['config' => $config]]);
}
}
}