custom/static-plugins/PixupWishlistSW6/src/Subscriber/CartSubscriber.php line 37

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