vendor/crehler/fast-navigation/src/Subscriber/FrontendSubscriber.php line 63

Open in your IDE?
  1. <?php
  2. namespace Crehler\FastNavigation\Subscriber;
  3. use Crehler\FastNavigation\Service\NavigationService;
  4. use Crehler\FastNavigation\Service\ProductService;
  5. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. class FrontendSubscriber implements EventSubscriberInterface
  10. {
  11.     const SESSION_FAST_NAVIGATION 'FastNavigation';
  12.     const SESSION_LAST_VISITED_CATEGORY 'LastVisitedCategory';
  13.     /**
  14.      * @var Session
  15.      */
  16.     private $session;
  17.     /**
  18.      * @var NavigationService;
  19.      */
  20.     private $navigationService;
  21.     /**
  22.      * @var ProductService
  23.      */
  24.     private $productService;
  25.     /**
  26.      * FrontendSubscriber constructor.
  27.      * @param Session $session
  28.      * @param NavigationService $navigationService
  29.      * @param ProductService $productService
  30.      */
  31.     public function __construct(Session $sessionNavigationService $navigationServiceProductService $productService)
  32.     {
  33.         $this->session $session;
  34.         $this->navigationService $navigationService;
  35.         $this->productService $productService;
  36.     }
  37.     /**
  38.      * @return array
  39.      */
  40.     public static function getSubscribedEvents() :array
  41.     {
  42.         return [
  43.             ProductListingResultEvent::class => 'onProductListingResultEvent',
  44.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  45.         ];
  46.     }
  47.     /**
  48.      * @param ProductListingResultEvent $event
  49.      */
  50.     public function onProductListingResultEvent(ProductListingResultEvent $event)
  51.     {
  52.         $navigationID $event->getResult()->getCurrentFilter('navigationId');
  53.         $this->session->set(self::SESSION_LAST_VISITED_CATEGORY$navigationID);
  54.         $session $this->session->get(self::SESSION_FAST_NAVIGATION);
  55.         $elements $event->getResult()->getElements();
  56.         $navigationProducts = [];
  57.         if($session !== null)
  58.         {
  59.             $navigationProducts $session;
  60.         }
  61.         unset($navigationProducts[$navigationID]);
  62.         foreach ($elements as $element){
  63.             $navigationProducts[$navigationID][] = $element->getId();
  64.         }
  65.         $this->session->set(self::SESSION_FAST_NAVIGATION$navigationProducts);
  66.     }
  67.     /**
  68.      * @param ProductPageLoadedEvent $event
  69.      */
  70.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
  71.     {
  72.         $productID $event->getPage()->getProduct()->getId();
  73.         $lastVisitedCategory $this->session->get(self::SESSION_LAST_VISITED_CATEGORY);
  74.         $navigationProducts $this->session->get(self::SESSION_FAST_NAVIGATION);
  75.         if(!in_array($lastVisitedCategory$event->getPage()->getProduct()->getCategoryTree())) {
  76.             $lastVisitedCategory null;
  77.         }
  78.         if(!empty($navigationProducts) && isset($navigationProducts[$lastVisitedCategory])) {
  79.             $navigationProducts $navigationProducts[$lastVisitedCategory];
  80.             $this->navigationService->setArray($navigationProducts);
  81.             $prevProduct $this->navigationService->prev($productID);
  82.             $nextProduct $this->navigationService->next($productID);
  83.             if($prevProduct) {
  84.                 $event->getPage()->addExtension(
  85.                     'ProductNavigation',
  86.                     $event->getPage()->assign([
  87.                         'prevProduct' => $prevProduct
  88.                     ])
  89.                 );
  90.             }
  91.             if($nextProduct) {
  92.                 $event->getPage()->addExtension(
  93.                     'ProductNavigation',
  94.                     $event->getPage()->assign([
  95.                         'nextProduct' => $nextProduct
  96.                     ])
  97.                 );
  98.             }
  99.         }else{
  100.             $productCategoryTreeID $event->getPage()->getProduct()->getCategoryTree();
  101.             $productCategoryID $productCategoryTreeID[1];
  102.             $navigationProducts $this->productService->getProducts($productCategoryID);
  103.             $this->navigationService->setArray($navigationProducts);
  104.             $prevProduct $this->navigationService->prev($productID);
  105.             $nextProduct $this->navigationService->next($productID);
  106.             if($prevProduct) {
  107.                 $event->getPage()->addExtension(
  108.                     'ProductNavigation',
  109.                     $event->getPage()->assign([
  110.                         'prevProduct' => $prevProduct
  111.                     ])
  112.                 );
  113.             }
  114.             if($nextProduct) {
  115.                 $event->getPage()->addExtension(
  116.                     'ProductNavigation',
  117.                     $event->getPage()->assign([
  118.                         'nextProduct' => $nextProduct
  119.                     ])
  120.                 );
  121.             }
  122.         }
  123.     }
  124. }