<?php
declare(strict_types=1);
namespace Orcamultimedia\OciPunchout\Subscriber;
use Orcamultimedia\OciPunchout\Service\OciService;
use Orcamultimedia\OciPunchout\Struct\OciPunchoutData;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutCartSubscriber implements EventSubscriberInterface
{
private OciService $ociService;
public function __construct(
OciService $ociService
)
{
$this->ociService = $ociService;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'addOciPunchoutPageData',
];
}
public function addOciPunchoutPageData(CheckoutCartPageLoadedEvent $event): void
{
if (!$this->ociService->isPunchout()) {
return;
}
$page = $event->getPage();
$request = $event->getRequest();
$punchoutData = new OciPunchoutData();
$oci = $request->getSession()->get('oci');
$punchoutData->assign([
'hookUrl' => $oci['hookUrl'],
'returnTarget' => $oci['returnTarget'],
'currencyIsoCode' => $page->getHeader()->getActiveCurrency()->getIsoCode(),
'lineItems' => $page->getCart()->getLineItems()
]);
$page->addExtension(OciPunchoutData::EXTENSION_NAME, $punchoutData);
}
}