custom/static-plugins/SamsonCustomer/src/Subscriber/CartSubscriber.php line 80

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2020
  11.  *
  12.  ***/
  13. use Samson\Api\TypePlate\Controller\MyProductsController;
  14. use Samson\Service\PersistentCartService;
  15. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  16. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemQuantityChangedEvent;
  17. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  18. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21.  * Class CartSubscriber
  22.  * @package Samson\Subscriber
  23.  * @author Artur Seitz <artur.seitz@dkd.de>
  24.  */
  25. class CartSubscriber implements EventSubscriberInterface
  26. {
  27.     private PersistentCartService $persistentCartService;
  28.     public function __construct(PersistentCartService $persistentCartService)
  29.     {
  30.         $this->persistentCartService $persistentCartService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             BeforeLineItemAddedEvent::class => 'onLineItemAddedEvent',
  36.             BeforeLineItemQuantityChangedEvent::class => 'onLineItemQuantityChangedEvent',
  37.             BeforeLineItemRemovedEvent::class => 'onLineItemRemovedEvent',
  38.             //OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoadedEvent'
  39.         ];
  40.     }
  41.     public function onOffcanvasCartPageLoadedEvent(OffcanvasCartPageLoadedEvent $event)
  42.     {
  43.         if($event->getCart()->getName() === MyProductsController::MY_PRODUCTS_CART_NAME){
  44.             return;
  45.         }
  46.         $cart $event->getPage()->getCart();
  47.         $hideOptions = [
  48.             'nicht vorhanden',
  49.             'nicht gefordert',
  50.             'ohne besondere maßnahmen',
  51.             'ohne',
  52.             'not required',
  53.             'not present',
  54.             'without',
  55.             'without special instructions',
  56.             '-'
  57.         ];
  58.         foreach ($cart->getLineItems() as $lineItem) {
  59.             $payloadOptions = [];
  60.             $options $lineItem->getPayload()['options'];
  61.             foreach ($options as $option) {
  62.                 if (!in_array(strtolower($option['option']), $hideOptions)) {
  63.                     $payloadOptions[] = $option;
  64.                 }
  65.             }
  66.             $lineItem->setPayloadValue('options'$payloadOptions);
  67.         }
  68.     }
  69.     public function onLineItemAddedEvent(BeforeLineItemAddedEvent $event): void
  70.     {
  71.         if($event->getCart()->getName() === MyProductsController::MY_PRODUCTS_CART_NAME){
  72.             return;
  73.         }
  74.         if (empty($event->getSalesChannelContext()->getCustomer())) {
  75.             return;
  76.         }
  77.         $this->persistentCartService->refreshPersistentCart(
  78.             $event->getSalesChannelContext()->getCustomer(),
  79.             $event->getCart(),
  80.             $event->getSalesChannelContext()->getContext()
  81.         );
  82.     }
  83.     public function onLineItemQuantityChangedEvent(BeforeLineItemQuantityChangedEvent $event): void
  84.     {
  85.         if (empty($event->getSalesChannelContext()->getCustomer())) {
  86.             return;
  87.         }
  88.         $this->persistentCartService->refreshPersistentCart(
  89.             $event->getSalesChannelContext()->getCustomer(),
  90.             $event->getCart(),
  91.             $event->getSalesChannelContext()->getContext()
  92.         );
  93.     }
  94.     public function onLineItemRemovedEvent(BeforeLineItemRemovedEvent $event): void
  95.     {
  96.         if($event->getCart()->getName() === MyProductsController::MY_PRODUCTS_CART_NAME){
  97.             return;
  98.         }
  99.         if (empty($event->getSalesChannelContext()->getCustomer())) {
  100.             return;
  101.         }
  102.         $this->persistentCartService->refreshPersistentCart(
  103.             $event->getSalesChannelContext()->getCustomer(),
  104.             $event->getCart(),
  105.             $event->getSalesChannelContext()->getContext()
  106.         );
  107.     }
  108. }