custom/plugins/HoggiTheme/src/Storefront/Page/AllPagesCartSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SchmelzerMedien\HoggiTheme\Storefront\Page;
  3. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Struct\ArrayStruct;
  6. use Shopware\Core\Framework\Uuid\Uuid;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  11. use Shopware\Storefront\Page\LandingPage\LandingPageLoadedEvent;
  12. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class AllPagesCartSubscriber implements EventSubscriberInterface
  18. {
  19.     private CartService $cartService;
  20.     public function __construct(CartService $cartService)
  21.     {
  22.         $this->cartService $cartService;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         $events = [
  27.             GenericPageLoadedEvent::class,
  28.             ProductPageLoadedEvent::class,
  29.             NavigationPageLoadedEvent::class,
  30.             LandingPageLoadedEvent::class,
  31.             SearchPageLoadedEvent::class,
  32.             CheckoutCartPageLoadedEvent::class,
  33.             CheckoutRegisterPageLoadedEvent::class,
  34.             CheckoutConfirmPageLoadedEvent::class,
  35.         ];
  36.         return array_fill_keys($events'onPageLoaded');
  37.     }
  38.     public function onPageLoaded($event): void
  39.     {
  40.         /** @var SalesChannelContext $salesChannelContext */
  41.         $salesChannelContext $event->getSalesChannelContext();
  42.         $cart $this->cartService->getCart(
  43.             $salesChannelContext->getToken(),
  44.             $salesChannelContext
  45.         );
  46.         $page $event->getPage();
  47.         $page->assign([
  48.             'cart' => $cart,
  49.         ]);
  50.     }
  51. }