<?php
namespace Onedot\Asus\Subscriber;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
/**
* @var CartService
*/
private $cartService;
public function __construct(CartService $cartService)
{
$this->cartService = $cartService;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
$saleChannelContext = $event->getSalesChannelContext();
$page = $event->getPage();
$cart = $this->cartService->getCart($saleChannelContext->getToken(), $saleChannelContext);
$page->assign([
'cart' => $cart,
]);
}
}