<?php declare(strict_types=1);
/**
* GourmopsProductBenefits
* Copyright (c) Simone Gottwald GOURMOPS
*/
namespace GourmopsProductBenefits\Subscriber;
use GourmopsProductBenefits\GourmopsProductBenefits;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\Product\ProductPage;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
/**
* Class ProductPageSubscriber
*/
class ProductPageSubscriber extends AbstractSubscriber
{
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
/**
* @param ProductPageLoadedEvent $event
*
* @return void
*/
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
/** @var array $config */
$config = $this->getConfig($event->getSalesChannelContext());
/** @var ProductPage $page */
$page = $event->getPage();
/** @var array|null $customFields */
$customFields = $this->getCustomFields($page->getProduct(), $event->getContext());
if (empty($config) ||
empty($config['activated']) ||
empty($customFields)
) {
return;
}
/** @var ArrayStruct $arrayStruct */
$arrayStruct = new ArrayStruct();
$arrayStruct->set(
'config',
[
'custom_fields' => $customFields,
]
);
$page->addExtension(GourmopsProductBenefits::FIELD_PLACEHOLDER, $arrayStruct);
}
}