custom/plugins/PremsWishlist/src/Storefront/Page/Subscriber/PageSubscriber.php line 107

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * PremSoft
  4.  * Copyright © 2019 Premsoft - Sven Mittreiter
  5.  *
  6.  * @copyright  Copyright (c) 2019, premsoft - Sven Mittreiter (http://www.premsoft.de)
  7.  * @author     Sven Mittreiter <info@premsoft.de>
  8.  */
  9. namespace Prems\Plugin\PremsWishlist\Storefront\Page\Subscriber;
  10. use Prems\Plugin\PremsWishlist\Core\Wishlist\Storefront\ConfigService;
  11. use Prems\Plugin\PremsWishlist\Core\Wishlist\Storefront\WishlistService;
  12. use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
  13. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  15. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  17. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  18. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  19. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Prems\Plugin\PremsWishlist\Storefront\Page\Listing\ListingPageLoader;
  22. use Symfony\Component\HttpFoundation\Session\Session;
  23. class PageSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var ConfigService
  27.      */
  28.     private $configService;
  29.     /**
  30.      * @var ListingPageLoader
  31.      */
  32.     private $listingPageLoader;
  33.     /**
  34.      * @var WishlistService
  35.      */
  36.     private $wishlistService;
  37.     /**
  38.      * @var Session
  39.      */
  40.     private $sessionService;
  41.     public function __construct(
  42.         ConfigService $configService,
  43.         ListingPageLoader $listingPageLoader,
  44.         WishlistService $wishlistService
  45.     ) {
  46.         $this->configService $configService;
  47.         $this->listingPageLoader  $listingPageLoader;
  48.         $this->wishlistService $wishlistService;
  49.         $this->sessionService = new Session();
  50.     }
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
  55.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
  56.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasPageLoadedEvent',
  57.             NavigationPageLoadedEvent::class => 'onNavigationPageLoadedEvent',
  58.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoadedEvent',
  59.             CustomerLoginEvent::class => 'onCustomerLoginEvent',
  60.             //CustomerBeforeLoginEvent::class => 'onCustomerBeforeLoginEvent'
  61.         ];
  62.     }
  63.     /**
  64.      * Save wishlists and products of customer (if created before login)
  65.      * @param CustomerBeforeLoginEvent $event
  66.      */
  67.     public function onCustomerBeforeLoginEvent($event): void
  68.     {
  69.         //$_SESSION['wishlistToken'] = $event->getSalesChannelContext()->getToken();
  70.     }
  71.     /**
  72.      * Save wishlists and products of customer (if created before login)
  73.      * @param CustomerLoginEvent $event
  74.      */
  75.     public function onCustomerLoginEvent($event): void
  76.     {
  77.         $wishlistSettings $this->configService->getConfig();
  78.         if ($wishlistSettings->isLoginRequired()) {
  79.             return;
  80.         }
  81.         $context $event->getSalesChannelContext();
  82.         $customer $event->getCustomer();
  83.         $token $this->sessionService->get('wishlistToken');
  84.         $this->sessionService->remove('wishlistToken');
  85.         $this->wishlistService->saveTokenWishlistsInDatabase($context$customer$token);
  86.     }
  87.     /**
  88.      * Load Wishlist settings for wishlist icon in header
  89.      * @param HeaderPageletLoadedEvent $event
  90.      *
  91.      * @throws InconsistentCriteriaIdsException
  92.      */
  93.     public function onHeaderPageletLoadedEvent($event): void
  94.     {
  95.         $wishlistSettings null;
  96.         if ($event instanceof HeaderPageletLoadedEvent) {
  97.             $wishlistSettings $this->configService->getConfig();
  98.         } else {
  99.             return;
  100.         }
  101.         if (!$wishlistSettings) {
  102.             return;
  103.         }
  104.         $context $event->getSalesChannelContext();
  105.         $user $context->getCustomer();
  106.         // Set Wishlist Token if not logged in
  107.         if (!$this->sessionService->get('wishlistToken') && !$user) {
  108.             $this->sessionService->set('wishlistToken'$event->getSalesChannelContext()->getToken());
  109.         }
  110.         $event->getPagelet()->addExtension('premsWishlist'$wishlistSettings);
  111.     }
  112.     /**
  113.      * Show wishlist button in article listing
  114.      * @param NavigationPageLoadedEvent $event
  115.      *
  116.      * @throws InconsistentCriteriaIdsException
  117.      */
  118.     public function onNavigationPageLoadedEvent($event): void
  119.     {
  120.         $request $event->getRequest();
  121.         $context $event->getSalesChannelContext();
  122.         $wishlistSettings null;
  123.         if ($event instanceof NavigationPageLoadedEvent) {
  124.             $wishlistSettings $this->configService->getConfig();
  125.         } else {
  126.             return;
  127.         }
  128.         if (!$wishlistSettings) {
  129.             return;
  130.         }
  131.         $settingVars $wishlistSettings->getVars();
  132.         if (!$settingVars['addToWishlistInListing']) {
  133.             return;
  134.         }
  135.         $event->getPage()->addExtension('premsWishlist'$wishlistSettings);
  136.     }
  137.     /**
  138.      * Show wishlist functionality in offcanvas cart
  139.      * @param OffcanvasCartPageLoadedEvent $event
  140.      *
  141.      * @throws InconsistentCriteriaIdsException
  142.      */
  143.     public function onOffcanvasPageLoadedEvent($event): void
  144.     {
  145.         $request $event->getRequest();
  146.         $context $event->getSalesChannelContext();
  147.         $wishlistSettings null;
  148.         if ($event instanceof OffcanvasCartPageLoadedEvent) {
  149.             $wishlistSettings $this->configService->getConfig();
  150.         } else {
  151.             return;
  152.         }
  153.         if (!$wishlistSettings) {
  154.             return;
  155.         }
  156.         $settingVars $wishlistSettings->getVars();
  157.         if (array_key_exists('showWishlistsInOffCanvas'$settingVars) && $settingVars['showWishlistsInOffCanvas']) {
  158.             return;
  159.         }
  160.         if (!$context->getCustomer() && $settingVars['loginRequired']) {
  161.             return;
  162.         }
  163.         $wishlists $this->listingPageLoader->load($request$context);
  164.         $maxNumberWishlistsPerUser = (int) $wishlistSettings->getMaxNumberWishlistsPerUser();
  165.         if ($maxNumberWishlistsPerUser == || $wishlists->getListing()->count() < $maxNumberWishlistsPerUser) {
  166.             $maxWishlistNumberReached false;
  167.         } else {
  168.             $maxWishlistNumberReached true;
  169.         }
  170.         $event->getPage()->assign(['maxWishlistNumberReached' => $maxWishlistNumberReached'premsWishlistLoggedIn' => 1]);
  171.         $event->getPage()->addExtension('wishlists'$wishlists);
  172.         $event->getPage()->addExtension('premsWishlist'$wishlistSettings);
  173.     }
  174.     /**
  175.      * Show wishlist functionality in cart
  176.      * @param CheckoutCartPageLoadedEvent $event
  177.      *
  178.      * @throws InconsistentCriteriaIdsException
  179.      */
  180.     public function onCheckoutCartPageLoadedEvent($event): void
  181.     {
  182.         $request $event->getRequest();
  183.         $context $event->getSalesChannelContext();
  184.         $wishlistSettings null;
  185.         if ($event instanceof CheckoutCartPageLoadedEvent) {
  186.             $wishlistSettings $this->configService->getConfig();
  187.         } else {
  188.             return;
  189.         }
  190.         if (!$wishlistSettings) {
  191.             return;
  192.         }
  193.         $settingVars $wishlistSettings->getVars();
  194.         if (!$settingVars['showWishlistsInBasket']) {
  195.             return;
  196.         }
  197.         if (!$context->getCustomer() && $settingVars['loginRequired']) {
  198.             $event->getPage()->assign(['premsWishlistLoggedIn' => 0]);
  199.             $event->getPage()->addExtension('premsWishlist'$wishlistSettings);
  200.             return;
  201.         }
  202.         $wishlists $this->listingPageLoader->load($request$context);
  203.         $maxNumberWishlistsPerUser = (int) $wishlistSettings->getMaxNumberWishlistsPerUser();
  204.         if ($maxNumberWishlistsPerUser == || $wishlists->getListing()->count() < $maxNumberWishlistsPerUser) {
  205.             $maxWishlistNumberReached false;
  206.         } else {
  207.             $maxWishlistNumberReached true;
  208.         }
  209.         $event->getPage()->assign(['maxWishlistNumberReached' => $maxWishlistNumberReached'premsWishlistLoggedIn' => 1]);
  210.         $event->getPage()->addExtension('wishlists'$wishlists);
  211.         $event->getPage()->addExtension('premsWishlist'$wishlistSettings);
  212.     }
  213.     /**
  214.      * Show wishlist functionality in checkout
  215.      * @param CheckoutCartPageLoadedEvent $event
  216.      *
  217.      * @throws InconsistentCriteriaIdsException
  218.      */
  219.     public function onCheckoutConfirmPageLoadedEvent($event): void
  220.     {
  221.         $request $event->getRequest();
  222.         $context $event->getSalesChannelContext();
  223.         $wishlistSettings null;
  224.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  225.             $wishlistSettings $this->configService->getConfig();
  226.         } else {
  227.             return;
  228.         }
  229.         if (!$wishlistSettings) {
  230.             return;
  231.         }
  232.         if (!$context->getCustomer() && $wishlistSettings->isLoginRequired()) {
  233.             return;
  234.         }
  235.         $settingVars $wishlistSettings->getVars();
  236.         if (!$settingVars['showWishlistsInCheckout']) {
  237.             return;
  238.         }
  239.         $wishlists $this->listingPageLoader->load($request$contexttrue);
  240.         $maxNumberWishlistsPerUser = (int) $wishlistSettings->getMaxNumberWishlistsPerUser();
  241.         if ($maxNumberWishlistsPerUser == || $wishlists->getListing()->count() < $maxNumberWishlistsPerUser) {
  242.             $maxWishlistNumberReached false;
  243.         } else {
  244.             $maxWishlistNumberReached true;
  245.         }
  246.         $event->getPage()->assign(['maxWishlistNumberReached' => $maxWishlistNumberReached'premsWishlistLoggedIn' => 1]);
  247.         $event->getPage()->addExtension('wishlists'$wishlists);
  248.         $event->getPage()->addExtension('premsWishlist'$wishlistSettings);
  249.     }
  250. }