<?php declare(strict_types=1);
namespace SchmelzerMedien\HoggiTheme\Storefront\Page;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\LandingPage\LandingPageLoadedEvent;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AllPagesCartSubscriber implements EventSubscriberInterface
{
private CartService $cartService;
public function __construct(CartService $cartService)
{
$this->cartService = $cartService;
}
public static function getSubscribedEvents(): array
{
$events = [
GenericPageLoadedEvent::class,
ProductPageLoadedEvent::class,
NavigationPageLoadedEvent::class,
LandingPageLoadedEvent::class,
SearchPageLoadedEvent::class,
CheckoutCartPageLoadedEvent::class,
CheckoutRegisterPageLoadedEvent::class,
CheckoutConfirmPageLoadedEvent::class,
];
return array_fill_keys($events, 'onPageLoaded');
}
public function onPageLoaded($event): void
{
/** @var SalesChannelContext $salesChannelContext */
$salesChannelContext = $event->getSalesChannelContext();
$cart = $this->cartService->getCart(
$salesChannelContext->getToken(),
$salesChannelContext
);
$page = $event->getPage();
$page->assign([
'cart' => $cart,
]);
}
}