<?php declare(strict_types=1);
/**
* digitvision
*
* @category digitvision
* @package Shopware\Plugins\DvsnBundle
* @copyright (c) 2021 digitvision
*/
namespace Dvsn\Bundle\Subscriber\Storefront\Page\Product;
use Dvsn\Bundle\Service\Bundle\BundleCollectorServiceInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageSubscriber implements EventSubscriberInterface
{
private BundleCollectorServiceInterface $bundleCollectorService;
private SystemConfigService $systemConfigService;
public function __construct(
BundleCollectorServiceInterface $bundleCollectorService,
SystemConfigService $systemConfigService
) {
$this->bundleCollectorService = $bundleCollectorService;
$this->systemConfigService = $systemConfigService;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onPageLoaded'
];
}
/**
* ...
*
* @param ProductPageLoadedEvent $event
*/
public function onPageLoaded(ProductPageLoadedEvent $event)
{
// plugin has to be active
if ((boolean) $this->systemConfigService->get('DvsnBundle.config.status', $event->getSalesChannelContext()->getSalesChannel()->getId()) === false) {
// nothing to do
return;
}
// get params
$salesChannelContext = $event->getSalesChannelContext();
$product = $event->getPage()->getProduct();
$request = $event->getRequest();
// get the product options
$bundles = $this->bundleCollectorService->get(
$product,
$salesChannelContext
);
// assign to page
$event->getPage()->assign([
'dvsnBundle' => $bundles,
'dvsnBundleConfiguration' => $this->systemConfigService->get('DvsnBundle.config', $salesChannelContext->getSalesChannel()->getId())
]);
}
}