<?php
declare(strict_types=1);
namespace LDSCustom\Subscriber;
use LDSCustom\Service\VariantPrices;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Product implements EventSubscriberInterface
{
private $service;
public function __construct(VariantPrices $service)
{
$this->service = $service;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$configuratorSettings = $event->getPage()->getConfiguratorSettings();
$product = $event->getPage()->getProduct();
if (0 === $configuratorSettings->count()) {
return;
}
$options = $this->service->getVariantPrices(
$product,
$configuratorSettings->getElements(),
$event->getSalesChannelContext()
);
if ([] === $options) {
return;
}
$event->getPage()->assign(['ldsVariantPricesOptions' => $options['available'],]);
}
}