custom/static-plugins/PixupWishlistSW6/src/Subscriber/ProductDetailSubscriber.php line 86

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Pixup\Wishlist\Subscriber;
  3. use Pixup\Wishlist\Core\Boot;
  4. use Pixup\Wishlist\Entitys\Model\WishlistModel;
  5. use Pixup\Wishlist\Model\WishlistSessionHandler;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  7. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  9. use Shopware\Core\Content\Cms\CmsPageEntity;
  10. use Shopware\Core\Content\Cms\CmsPageEvents;
  11. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  12. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductListingStruct;
  13. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  14. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  15. use Shopware\Core\Content\Product\ProductEntity;
  16. use Shopware\Core\Content\Product\ProductEvents;
  17. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  19. use Shopware\Storefront\Page\PageLoadedEvent;
  20. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  21. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
  25. class ProductDetailSubscriber implements EventSubscriberInterface
  26. {
  27.     private $boot;
  28.     /** @var WishlistSessionHandler */
  29.     private $wishlistSessionHandler;
  30.     public function __construct(Boot $boot,RequestStack $requestStackWishlistSessionHandler $wishlistSessionHandler)
  31.     {
  32.         $this->boot $boot;
  33.         //set sessionID because shopware does not set it if its an API call
  34.         $token substr(bin2hex($requestStack->getCurrentRequest()->headers->get("sw-context-token")),0,32);
  35.         if(isset($token) && !isset($_SESSION['_sf2_attributes']['sessionId'])) {
  36.             $_SESSION['_sf2_attributes'] = array('sessionId' =>$token);
  37.         }
  38.         $this->wishlistSessionHandler $wishlistSessionHandler;
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             ##need to take this methods because they provide a salesChannelContext / userID
  44.             ProductPageLoadedEvent::class => 'addWishlistInformation',
  45.             CmsPageLoadedEvent::class => "addWishlistInformationToProductListing",
  46.             HeaderPageletLoadedEvent::class => "pageLoaded"
  47.         ];
  48.     }
  49.     public function pageLoaded(HeaderPageletLoadedEvent $event){
  50.         //get the count of all products to display it on the header pagelet
  51.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  52.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  53.         $id $wishListEntityHandler->getWishlistCustomerId(
  54.             $event->getSalesChannelContext(),
  55.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  56.             $this->wishlistSessionHandler->getSessionId()
  57.         );
  58.         $count 0;
  59.         if($id == null)
  60.             $count 0;
  61.         else {
  62.             $wishlists $wishListEntityHandler->getWishlists($salesChannelId$id$event->getSalesChannelContext(),""""""true);
  63.             /**
  64.              * @var WishlistModel $wishlist
  65.              */
  66.             foreach ($wishlists as $wishlist) {
  67.                 $count += count($wishlist->getProducts()->getIds());
  68.             }
  69.         }
  70.         $event->getPagelet()->assign([
  71.             "pixup"=>[
  72.                 "wishlistProductCount"=>$count,
  73.                 "showIconOnProductImage"=>$this->boot->getConfig()['showOnImage']
  74.             ]
  75.         ]);
  76.     }
  77.     public function addWishlistInformationToProductListing(CmsPageLoadedEvent $event){
  78.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  79.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  80.         $id $wishListEntityHandler->getWishlistCustomerId(
  81.             $event->getSalesChannelContext(),
  82.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  83.             $this->wishlistSessionHandler->getSessionId()
  84.         );
  85.         if($id == null)
  86.             return;
  87.         //get the listing pages
  88.         /**
  89.          * @var CmsPageEntity $cmsEntity
  90.          */
  91.         if($event->getResult() == null)
  92.             return;
  93.         foreach($event->getResult() as $cmsEntity){
  94.             $type =  $cmsEntity->getType();
  95.             if($type !== "product_list" && $type !== "page" )
  96.                 continue;
  97.             $sections $cmsEntity->getSections();
  98.             if($sections == null)
  99.                 continue;
  100.             /**
  101.              * @var CmsSectionEntity $section
  102.              */
  103.             foreach($sections as $section){
  104.                 $blocks $section->getBlocks();
  105.                 if($blocks == null)
  106.                     continue;
  107.                 /**
  108.                  * @var CmsBlockEntity $block
  109.                  */
  110.                 foreach($blocks as $block){
  111.                     if($block->getType()!=='product-listing' && $block->getType()!=="product-slider")
  112.                         continue;
  113.                     $slots $block->getSlots();
  114.                     /**
  115.                      * @var CmsSlotEntity $slot
  116.                      */
  117.                     foreach($slots as $slot){
  118.                         if($slot->getType()!=='product-listing' && $slot->getType()!=='product-slider')
  119.                             continue;
  120.                         /**
  121.                          * @var ProductListingStruct $data
  122.                          */
  123.                         $data $slot->getData();
  124.                         if($data instanceof ProductListingStruct){
  125.                             if($data->getListing()==null)
  126.                                 continue;
  127.                             $products $data->getListing()->getEntities();
  128.                         }elseif($data instanceof ProductSliderStruct){
  129.                             if($data->getProducts() == null)
  130.                                 continue;
  131.                             $products $data->getProducts();
  132.                         }
  133.                         if($data instanceof ProductSliderStruct || $data instanceof ProductListingStruct)
  134.                             /**
  135.                              * @var ProductEntity $product
  136.                              */
  137.                             foreach($products as $product){
  138.                                 $productID $product->getId();
  139.                                 $product->setCustomFields(
  140.                                     [
  141.                                         'isOnWishlist'=>$this->boot->getFacade()->getWishlistEntityHandler()->checkIfProductExists($productID,$salesChannelId,$id$event->getSalesChannelContext()),
  142.                                         "showIconOnProductImage"=>$this->boot->getConfig()['showOnImage']
  143.                                     ]
  144.                                 );
  145.                             }
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.     }
  151.     /**
  152.      * @param ProductPageLoadedEvent $event
  153.      * @description to add the wishlist state to the product detail site
  154.      */
  155.     public function addWishlistInformation(ProductPageLoadedEvent $event):void {
  156.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  157.         $productID $event->getPage()->getProduct()->getId();
  158.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  159.         $id $wishListEntityHandler->getWishlistCustomerId(
  160.             $event->getSalesChannelContext(),
  161.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  162.             $this->wishlistSessionHandler->getSessionId()
  163.         );
  164.         if($id == null)
  165.             return;
  166.         $onWishlist $this->boot->getFacade()->getWishlistEntityHandler()->checkIfProductExists($productID,$salesChannelId,$id,$event->getSalesChannelContext());
  167.         $extensions $event->getPage()->getProduct()->getExtensions();
  168.         $extensions["pixup_wish_list_state"]['isOnWishlist'] = $onWishlist;
  169.         $event->getPage()->getProduct()->setExtensions($extensions);
  170.     }
  171. }