src/PentagastBundle/Subscriber/StorefrontSubscriber.php line 65

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * valantic CEC Deutschland GmbH
  4.  * Copyright (c) 2022.
  5.  *
  6.  * All rights reserved.
  7.  *
  8.  * @see https://www.valantic.com/
  9.  *
  10.  * @author andrei
  11.  * Date: 12.10.2022 16:18
  12.  */
  13. namespace Valantic\Bundle\PentagastBundle\Subscriber;
  14. use Shopware\Core\Checkout\Cart\Cart;
  15. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  16. use Shopware\Storefront\Event\StorefrontRenderEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Symfony\Component\Routing\RouterInterface;
  22. use Valantic\Bundle\PentagastBundle\Service\ShopFeaturesService;
  23. use Valantic\Bundle\PentagastBundle\Struct\PurchasePrice;
  24. class StorefrontSubscriber implements EventSubscriberInterface
  25. {
  26.     public const SALES_PRICES_VISIBLE 'salesPricesVisible';
  27.     public const STOCK_LEVELS_VISIBLE 'stockLevelsVisible';
  28.     public const PURCHASE_PRICES_VISIBLE 'purchasePricesVisible';
  29.     public const REGISTRATION_ENABLED 'registrationEnabled';
  30.     public const SEARCH_BAR_ENABLED 'searchBarEnabled';
  31.     public const LOGIN_ENABLED 'loginEnabled';
  32.     public const CART_CHECKOUT_ENABLED 'cartCheckoutEnabled';
  33.     public const DELIVERY_TIMES_VISIBLE 'showDeliveryTimes';
  34.     public const STOCK_TRAFFIC_LIGHTS_VISIBLE 'stockTrafficLightsVisible';
  35.     public const DIGTIAL_MEDIA_DOWNLOADS_VISIBLE 'digitalMediaDownloadsVisible';
  36.     
  37.     public const ACTIVATE_CLOSED_SHOP 'val_pentagast_bundle_closed_shop';
  38.     
  39.     private RouterInterface $router;    
  40.     public function __construct(private ShopFeaturesService $shopFeaturesServiceRouterInterface $router)
  41.     {
  42.         $this->router $router;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             StorefrontRenderEvent::class => ['onStorefrontRender', -250],
  48.         ];
  49.     }
  50.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  51.     {   
  52.         $salesChannelContext $event->getSalesChannelContext();
  53.         $customFields $salesChannelContext->getSalesChannel()->getCustomFields();
  54.         $closedShopEnabled $customFields[self::ACTIVATE_CLOSED_SHOP] ?? false;
  55.         $isLoggedIn $salesChannelContext->getCustomer() !== null;
  56.     
  57.         $allowedRoutes = [
  58.             'frontend.account.login.page',
  59.             'frontend.account.login',
  60.             'frontend.cms.page',
  61.             #'frontend.navigation.page',
  62.             'frontend.landing.page',
  63.             'frontend.content.page',
  64.         ];
  65.     
  66.         $request $event->getRequest();
  67.         $currentRoute $request->attributes->get('_route');
  68.     
  69.         // Nur Redirect ausführen, wenn es keine Ajax-Anfrage ist
  70.         if ($closedShopEnabled && !$isLoggedIn && (!in_array($currentRoute$allowedRoutestrue) || strpos($cmsPage->getTranslated()['name'], 'Datenschutz'))) {
  71.             if ($request->isXmlHttpRequest()) {
  72.                 $event->setParameter('error''Login erforderlich');
  73.             } else {
  74.                 $loginUrl $this->router->generate('frontend.account.login.page');
  75.                 header('Location: ' $loginUrl);
  76.                 exit;
  77.             }
  78.         }
  79.         
  80.         $event->setParameter(self::SALES_PRICES_VISIBLE$this->shopFeaturesService->salesPricesVisible($event->getSalesChannelContext()));
  81.         $event->setParameter(self::STOCK_LEVELS_VISIBLE$this->shopFeaturesService->stockLevelsVisible($event->getSalesChannelContext()));
  82.         $event->setParameter(self::PURCHASE_PRICES_VISIBLE$this->shopFeaturesService->purchasePricesVisible($event->getSalesChannelContext()));
  83.         $event->setParameter(self::DIGTIAL_MEDIA_DOWNLOADS_VISIBLE$this->shopFeaturesService->digitalMediaDownloadsVisible($event->getSalesChannelContext()));
  84.         $event->setParameter(self::REGISTRATION_ENABLED$this->shopFeaturesService->registrationEnabled($event->getSalesChannelContext()));
  85.         $event->setParameter(self::SEARCH_BAR_ENABLED$this->shopFeaturesService->searchBarEnabled($event->getSalesChannelContext()));
  86.         $event->setParameter(self::LOGIN_ENABLED$this->shopFeaturesService->loginEnabled($event->getSalesChannelContext()));
  87.         $event->setParameter(self::CART_CHECKOUT_ENABLED$this->shopFeaturesService->cartCheckoutEnabled($event->getSalesChannelContext()));
  88.         $event->setParameter(self::DELIVERY_TIMES_VISIBLE$this->shopFeaturesService->deliveryTimesVisible($event->getSalesChannelContext()));
  89.         $event->setParameter(self::STOCK_TRAFFIC_LIGHTS_VISIBLE$this->shopFeaturesService->stockTrafficLightsVisible($event->getSalesChannelContext()));
  90.         $this->updateCart($event->getParameters());
  91.     }
  92.     protected function updateCart(array $parameters): void
  93.     {
  94.         if ($page $parameters['page'] ?? null) {
  95.             if (method_exists($page'getCart')) {
  96.                 /** @var Cart $cart */
  97.                 $cart $page->getCart();
  98.                 foreach ($cart->getLineItems()->getElements() as $lineItem) {
  99.                     $purchasePriceValue 0;
  100.                     $targetProductLine $lineItem;
  101.                     if ($lineItem->getType() === 'customized-products') {
  102.                         $targetProductLine $lineItem
  103.                             ->getChildren()
  104.                             ->filter(fn (LineItem $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE)
  105.                             ->first();
  106.                     }
  107.                     if ($targetProductLine) {
  108.                         $payload $targetProductLine->getPayload();
  109.                         $purchasePrices json_decode($payload['purchasePrices'] ?? ''true);
  110.                         if ($purchasePrices) {
  111.                             $purchasePriceValue $purchasePrices['calculatedPrice'] ?? 0;
  112.                         }
  113.                     }
  114.                     $purchasePrice = new PurchasePrice();
  115.                     $purchasePrice->setValue($purchasePriceValue);
  116.                     /** @psalm-suppress DeprecatedMethod */
  117.                     $lineItem->addExtension('purchasePrice'$purchasePrice);
  118.                 }
  119.             }
  120.         }
  121.     }
  122. }