custom/plugins/PixupWishlistSW6/src/Subscriber/CartSubscriber.php line 109

Open in your IDE?
  1. <?php
  2. namespace Pixup\Wishlist\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  6. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  8. use Shopware\Storefront\Page\PageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Pixup\Wishlist\Core\Boot;
  11. use Shopware\Core\Checkout\Cart\Event\LineItemRemovedEvent;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class CartSubscriber implements EventSubscriberInterface
  14. {
  15.     private $boot;
  16.     public function __construct(Boot $boot,RequestStack $requestStack)
  17.     {
  18.         $this->boot $boot;
  19.         //set sessionID because shopware does not set it if its an API call
  20.         $token substr(bin2hex($requestStack->getCurrentRequest()->headers->get("sw-context-token")),0,32);
  21.         if(isset($token) && !isset($_SESSION['_sf2_attributes']['sessionId'])) {
  22.             $_SESSION['_sf2_attributes'] = array('sessionId' =>$token);
  23.         }
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             LineItemRemovedEvent::class => "removeFromBirthdayist",
  29.             CheckoutOrderPlacedEvent::class => "removeProductFromWishlist",
  30.             CheckoutCartPageLoadedEvent::class => "addInfoToCheckoutCart",
  31.             OffcanvasCartPageLoadedEvent::class => "addInfoToOffcanvasCart"
  32.         ];
  33.     }
  34.     /**
  35.      * @param CheckoutCartPageLoadedEvent $event
  36.      * @description adds information to lineItem
  37.      */
  38.     private function addInfoToCart(PageLoadedEvent $event){
  39.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  40.         if($this->boot->getConfig($salesChannelId)['fullAjaxMode'])
  41.             return;
  42.         $wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  43.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  44.         $customer $wishListEntityHandler->getWishlistCustomer(
  45.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  46.             ($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
  47.         );
  48.         $customerId $customer $customer->getId() : null;
  49.         if($customerId == null)
  50.             return;
  51.         if($this->boot->getConfig($salesChannelId)['showOnCart']) {
  52.             /**
  53.              * @var LineItem $lineItem
  54.              */
  55.             foreach ($event->getPage()->getCart()->getLineItems() as $lineItem) {
  56.                 $lineItem->setExtensions(array_merge($lineItem->getExtensions()??[],[
  57.                     'isOnWishlist' => $this->boot->getFacade()->getWishlistEntityHandler()->checkIfProductExists($lineItem->getId(), $salesChannelId$customerId)
  58.                 ]));
  59.             }
  60.         }
  61.     }
  62.     /**
  63.      * @param OffcanvasCartPageLoadedEvent $event
  64.      * @description adds configuration value to pagestruct
  65.      */
  66.     public function addInfoToOffcanvasCart(OffcanvasCartPageLoadedEvent $event): void{
  67.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  68.         $event->getPage()->setExtensions(array_merge($event->getPage()->getExtensions()??[],[
  69.             "pixup"=>[
  70.                 "showOnCart"=>$this->boot->getConfig($salesChannelId)['showOnOffcanvasCart'],
  71.                 "showOnCartImage"=>$this->boot->getConfig($salesChannelId)['showOnOffcanvasCartImage'],
  72.                 "antiCacheMode"=>$this->boot->getConfig($salesChannelId)['antiCacheMode'],
  73.                 "fullAjaxMode"=>$this->boot->getConfig($salesChannelId)['fullAjaxMode']
  74.             ]
  75.         ]));
  76.         $this->addInfoToCart($event);
  77.     }
  78.     /**
  79.      * @param CheckoutCartPageLoadedEvent $event
  80.      * @description adds configuration value to pagestruct
  81.      */
  82.     public function addInfoToCheckoutCart(CheckoutCartPageLoadedEvent $event): void{
  83.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  84.         $event->getPage()->setExtensions(array_merge($event->getPage()->getExtensions()??[],[
  85.             "pixup"=>[
  86.                 "showOnCart"=>$this->boot->getConfig($salesChannelId)['showOnCart'],
  87.                 "showOnCartImage"=>$this->boot->getConfig($salesChannelId)['showOnCartImage']
  88.             ]
  89.         ]));
  90.         $this->addInfoToCart($event);
  91.     }
  92.     /**
  93.      * @param LineItemRemovedEvent $event
  94.      * @description will check the birthday table and removes entrys from there if a lineItem is removed that whas added
  95.      * from a birthday wishlist
  96.      */
  97.     public function removeFromBirthdayist(LineItemRemovedEvent $event) :void{
  98.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  99.         $wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  100.         $productId $event->getLineItem()->getId();
  101.         $customer $wishListEntityHandler->getWishlistCustomer(
  102.             ($event->getSalesChannelContext()->getCustomer()==null)?null:$event->getSalesChannelContext()->getCustomer()->getId(),
  103.             ($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
  104.         );
  105.         $customerId $customer $customer->getId() :  null;
  106.         if($customerId == null)
  107.             return;
  108.         $wishListEntityHandler->removeUserFromBirthdayListByProductId($productId,$customerId);
  109.     }
  110.     /**
  111.      * @param CheckoutOrderPlacedEvent $event
  112.      * @description removes a product from the subscribed or owned birthday wishlist if customer completly orderd a item
  113.      */
  114.     public function removeProductFromWishlist(CheckoutOrderPlacedEvent $event){
  115.         $wishListEntityHandler $this->boot->getFacade()->getWishlistEntityHandler();
  116.         $wishListCookieHandler $this->boot->getFacade()->getCookieHandler();
  117.         $salesChannelId $event->getSalesChannelId();
  118.         if($event->getOrder()->getOrderCustomer()->getCustomer()->getGuest())
  119.             $customer null;
  120.         else
  121.             $customer $event->getOrder()->getOrderCustomer()->getCustomer();
  122.         $customer $wishListEntityHandler->getWishlistCustomer(
  123.             ($customer==null)?null:$customer->getId(),
  124.             ($wishListCookieHandler->getCookieId() == null)?null:$wishListCookieHandler->getCookieId()
  125.         );
  126.         try {
  127.             $customerId $customer $customer->getId() : null;
  128.             /**
  129.              * @var OrderLineItemEntity $orderLineItem
  130.              */
  131.             foreach($event->getOrder()->getLineItems()->getElements() as $orderLineItem){
  132.                 $wishlistId $wishListEntityHandler->removeUserFromBirthdayListByProductId($orderLineItem->getProductId(),$customerId);
  133.                 if(!empty($wishlistId))
  134.                     $wishListEntityHandler->deleteProductFromBirthdayWishlist([$orderLineItem->getProductId()],$wishlistId,$customerId,$salesChannelId);
  135.             }
  136.         }catch(\Exception $e){
  137.         }
  138.     }
  139. }