<?php declare(strict_types=1);
namespace WabsDigitalProductsExtended\Subscriber;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPage;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageLoaded implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageLoaded(PageLoadedEvent $event)
{
/** @var ProductPage $page */
$page = $event->getPage();
$product = $page->getProduct();
$customFields = $product->getCustomFields();
$showDownloads = true;
if (isset($customFields['igm_mbs_digital_content_access'])
&& count($customFields['igm_mbs_digital_content_access']) > 0) {
$currentCustomerGroup = $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
if (!in_array($currentCustomerGroup, $customFields['igm_mbs_digital_content_access'])) {
$showDownloads = false;
}
}
$page->addExtension('digitalProductsExtended', new ArrayStruct(['showProductDownloads' => $showDownloads]));
}
}