<?php
declare(strict_types=1);
namespace MadYoutubeVideoOptions\Subscriber;
use MadYoutubeVideoOptions\MadYoutubeVideoOptions;
use MadYoutubeVideoOptions\Service\MadCustomFieldService;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$product = $event->getPage()->getProduct();
$customFields = $product->getCustomFields();
if (isset($customFields['mad_product_youtube_options'])){
$youtube_options = str_replace(array("{", "}",'"'),"",$customFields['mad_product_youtube_options']);
$youtube_options = explode(",", $youtube_options);
$customFields['mad_product_youtube_options'] = array();
foreach($youtube_options as &$option){
$option = str_replace(array(" "),"",$option);
$option = explode(":", $option);
if($option[0]){
$customFields['mad_product_youtube_options'][$option[0]] = $option[1];
}
}
}
$product->setCustomFields($customFields);
}
}