<?php declare(strict_types=1);
namespace Su\Confi\Service;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class ConfiguratorData implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
public function __construct(SystemConfigService $systemConfigService)
{
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'addConfiguratorData'
];
}
public function addConfiguratorData(ProductPageLoadedEvent $event): void
{
$ct = $event->getRequest()->get('ct');
$vid = $event->getRequest()->get('vid');
$configuratorToken = !empty($ct) ? '&ct='.strip_tags($ct) : '';
$ConfiguratorVersionId = !empty($vid) ? '&vid='.strip_tags($vid) : '';
$productData = $event->getPage()->getProduct();
$confiHash = $productData->translated['customFields']['confi_id'] ?? null;
if(!empty($confiHash)){
$host = $this->systemConfigService->get('Confi.config.confiserver');
$ch = curl_init($host.'get.php?id='.$confiHash.$configuratorToken.$ConfiguratorVersionId);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
//
// var_dump($response);
// exit;
$confi = json_decode($response, true);
$confi['css'] = str_replace('confi.domondo.co.uk', 'jalousiescout.secom-united.com', $confi['css']);
$confi['js'] = str_replace('confi.domondo.co.uk', 'jalousiescout.secom-united.com', $confi['js']);
$confi['body'] = str_replace('confi.domondo.co.uk', 'jalousiescout.secom-united.com', $confi['body']);
$event->getPage()->assign(['confi' => $confi]);
$event->getPage()->assign(['product' => $productData]);
}
}
}