custom/plugins/PixupWishlistSW6/src/Subscriber/ProductDetailSubscriber.php line 96

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 Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  7. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  8. use Shopware\Core\Content\Cms\CmsPageEntity;
  9. use Shopware\Core\Content\Cms\CmsPageEvents;
  10. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  11. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductListingStruct;
  12. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  13. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  14. use Shopware\Core\Content\Product\ProductEntity;
  15. use Shopware\Core\Content\Product\ProductEvents;
  16. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  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.     public function __construct(Boot $boot,RequestStack $requestStack)
  29.     {
  30.         $this->boot $boot;
  31.         //set sessionID because shopware does not set it if its an API call
  32.         if ($requestStack->getCurrentRequest()->headers->get("sw-context-token") !== null) {
  33.             $token substr(bin2hex($requestStack->getCurrentRequest()->headers->get("sw-context-token")), 032);
  34.         } else {
  35.             $token "";
  36.         }
  37.         if(isset($token) && !isset($_SESSION['_sf2_attributes']['sessionId'])) {
  38.             $_SESSION['_sf2_attributes'] = array('sessionId' =>$token);
  39.         }
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             ##need to take this methods because they provide a salesChannelContext / userID
  45.             ProductPageLoadedEvent::class => 'addWishlistInformation',
  46.             CmsPageLoadedEvent::class => "addWishlistInformationToProductListing",
  47.             HeaderPageletLoadedEvent::class => "pageLoaded"
  48.         ];
  49.     }
  50.     public function pageLoaded(HeaderPageletLoadedEvent $event){
  51.         //get the count of all products to display it on the header pagelet
  52.         $count 0;
  53.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  54.         if(!$this->boot->getConfig($salesChannelId)['antiCacheMode'] || !$this->boot->getConfig($salesChannelId)['fullAjaxMode']) {
  55.             $wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  56.             $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  57.             $customer $wishListEntityHandler->getWishlistCustomer(
  58.                 ($event->getSalesChannelContext()->getCustomer() == null) ? null $event->getSalesChannelContext()->getCustomer()->getId(),
  59.                 ($wishListCookieHandler->getCookieId() == null) ? null $wishListCookieHandler->getCookieId()
  60.             );
  61.             $count 0;
  62.             $customerId $customer $customer->getId() : null;
  63.             if ($customerId == null)
  64.                 $count 0;
  65.             else {
  66.                 $wishlists $wishListEntityHandler->getWishlists($salesChannelId$customerId""""""true);
  67.                 /**
  68.                  * @var WishlistModel $wishlist
  69.                  */
  70.                 foreach ($wishlists as $wishlist) {
  71.                     $count += count($wishlist->getProducts()->getIds());
  72.                 }
  73.             }
  74.         }
  75.         $isLoggedIn = !(($event->getSalesChannelContext()->getCustomer() == null));
  76.         $event->getPagelet()->assign([
  77.             "pixup"=>[
  78.                 "wishlistProductCount"=>$count,
  79.                 "showIconOnProductImage"=>$this->boot->getConfig($salesChannelId)['showOnImage'],
  80.                 "antiCacheMode"=>$this->boot->getConfig($salesChannelId)['antiCacheMode'],
  81.                 "fullAjaxMode"=>$this->boot->getConfig($salesChannelId)['fullAjaxMode'],
  82.                 "allowMultipleWishlists" => $isLoggedIn || $this->boot->getConfig($salesChannelId)['enableMultiCookieUsage'],
  83.                 "allowSubscribeWishlist" => $isLoggedIn || $this->boot->getConfig($salesChannelId)['cookieUserCanSubscribe'],
  84.                 "requiredCookie" => (bool)$this->boot->getConfig($salesChannelId)['technicalRequiredCookie'],
  85.             ]
  86.         ]);
  87.     }
  88.     public function addWishlistInformationToProductListing(CmsPageLoadedEvent $event){
  89.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  90.         if($this->boot->getConfig($salesChannelId)['antiCacheMode'] || $this->boot->getConfig($salesChannelId)['fullAjaxMode'])
  91.             return;
  92.         $wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  93.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  94.         $customer $wishListEntityHandler->getWishlistCustomer(
  95.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  96.             ($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
  97.         );
  98.         $customerId $customer $customer->getId() : null;
  99.         if($customerId == null)
  100.             return;
  101.         //get the listing pages
  102.         /**
  103.          * @var CmsPageEntity $cmsEntity
  104.          */
  105.         if($event->getResult() == null)
  106.             return;
  107.         foreach($event->getResult() as $cmsEntity){
  108.             $type =  $cmsEntity->getType();
  109.             if($type !== "product_list" && $type !== "page" )
  110.                 continue;
  111.             $sections $cmsEntity->getSections();
  112.             if($sections == null)
  113.                 continue;
  114.             /**
  115.              * @var CmsSectionEntity $section
  116.              */
  117.             foreach($sections as $section){
  118.                 $blocks $section->getBlocks();
  119.                 if($blocks == null)
  120.                     continue;
  121.                 /**
  122.                  * @var CmsBlockEntity $block
  123.                  */
  124.                 foreach($blocks as $block){
  125.                     if($block->getType()!=='product-listing' && $block->getType()!=="product-slider")
  126.                         continue;
  127.                     $slots $block->getSlots();
  128.                     /**
  129.                      * @var CmsSlotEntity $slot
  130.                      */
  131.                     foreach($slots as $slot){
  132.                         if($slot->getType()!=='product-listing' && $slot->getType()!=='product-slider')
  133.                             continue;
  134.                         /**
  135.                          * @var ProductListingStruct $data
  136.                          */
  137.                         $data $slot->getData();
  138.                         if($data instanceof ProductListingStruct){
  139.                             if($data->getListing()==null)
  140.                                 continue;
  141.                             $products $data->getListing()->getEntities();
  142.                         }elseif($data instanceof ProductSliderStruct){
  143.                             if($data->getProducts() == null)
  144.                                 continue;
  145.                             $products $data->getProducts();
  146.                         }
  147.                         if($data instanceof ProductSliderStruct || $data instanceof ProductListingStruct)
  148.                             /**
  149.                              * @var ProductEntity $product
  150.                              */
  151.                             foreach($products as $product){
  152.                                 $productID $product->getId();
  153.                                 $product->setCustomFields(
  154.                                     array_merge($product->getCustomFields() ?? [], [
  155.                                         'isOnWishlist'=>$this->boot->getFacade()->getWishlistEntityHandler()->checkIfProductExists($productID,$salesChannelId,$customerId),
  156.                                         "showIconOnProductImage"=>$this->boot->getConfig($salesChannelId)['showOnImage']
  157.                                     ])
  158.                                 );
  159.                             }
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     }
  165.     /**
  166.      * @param ProductPageLoadedEvent $event
  167.      * @description to add the wishlist state to the product detail site
  168.      */
  169.     public function addWishlistInformation(ProductPageLoadedEvent $event):void {
  170.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  171.         if($this->boot->getConfig($salesChannelId)['antiCacheMode'] || $this->boot->getConfig($salesChannelId)['fullAjaxMode'])
  172.             return;
  173.         $wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  174.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  175.         $productID $event->getPage()->getProduct()->getId();
  176.         $customer $wishListEntityHandler->getWishlistCustomer(
  177.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  178.             ($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
  179.         );
  180.         $customerId $customer $customer->getId() : null;
  181.         if($customerId == null)
  182.             return;
  183.         $onWishlist $this->boot->getFacade()->getWishlistEntityHandler()->checkIfProductExists($productID,$salesChannelId,$customerId);
  184.         $event->getPage()->getProduct()->addArrayExtension('pixup_wish_list_state', ['isOnWishlist'=>$onWishlist]);
  185.     }
  186. }