custom/plugins/BstCatalogMode6/src/Storefront/Subscriber/Frontend.php line 140

Open in your IDE?
  1. <?php
  2. /**
  3.  * NOTICE OF LICENSE
  4.  *
  5.  * @copyright  Copyright (c) 01.03.2020 brainstation GbR
  6.  * @author     Mike Becker<[email protected]>
  7.  */
  8. declare(strict_types=1);
  9. namespace BstCatalogMode6\Storefront\Subscriber;
  10. use BstCatalogMode6\Struct\ConfigData;
  11. use Psr\Log\LoggerInterface;
  12. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  13. use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Shopware\Core\Checkout\Customer\CustomerEntity;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Core\PlatformRequest;
  19. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  20. use Shopware\Core\System\SystemConfig\SystemConfigService;
  21. use Shopware\Models\Customer\Customer;
  22. use Shopware\Storefront\Controller\CheckoutController;
  23. use Shopware\Storefront\Event\StorefrontRenderEvent;
  24. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  25. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
  26. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  31. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  32. use Symfony\Component\HttpKernel\KernelEvents;
  33. class Frontend implements EventSubscriberInterface
  34. {
  35.     const EXTENSION_NAME 'BstCatalogMode6';
  36.     /**
  37.      * @var SystemConfigService
  38.      */
  39.     private $systemConfigService;
  40.     /** @var RouterInterface */
  41.     private $router;
  42.     /** @var LoggerInterface */
  43.     private $logger;
  44.     /**
  45.      * Frontend constructor.
  46.      * @param SystemConfigService $systemConfigService
  47.      * @param RouterInterface $router
  48.      * @param LoggerInterface $logger
  49.      */
  50.     public function __construct(
  51.         SystemConfigService $systemConfigService,
  52.         RouterInterface $router,
  53.         LoggerInterface $logger
  54.     )
  55.     {
  56.         $this->systemConfigService $systemConfigService;
  57.         $this->router $router;
  58.         $this->logger $logger;
  59.     }
  60.     /**
  61.      * @return array
  62.      */
  63.     public static function getSubscribedEvents(): array
  64.     {
  65.         return [
  66.             StorefrontRenderEvent::class => 'onStorefrontRender',
  67.             ControllerArgumentsEvent::class => 'onControllerEvent',
  68.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  69.         ];
  70.     }
  71.     /**
  72.      * @param SalesChannelContext $context
  73.      * @param boolean $asArray
  74.      * @return ConfigData
  75.      */
  76.     private function getConfigData(SalesChannelContext $context$asArray false)
  77.     {
  78.         $config = new ConfigData($this->systemConfigService$context);
  79.         if (!$config->enabled) {
  80.             return;
  81.         }
  82.         /** @var CustomerEntity $customer */
  83.         $customer $context->getCustomer();
  84.         if ($customer !== null) {
  85.             $priceDisableGroup false;
  86.             if (is_array($config->hidePriceCustomerGroups)) {
  87.                 if (in_array($customer->getGroupId(), $config->hidePriceCustomerGroups)) {
  88.                     $priceDisableGroup true;
  89.                 }
  90.             }
  91.             $checkoutDisableGroup false;
  92.             if (is_array($config->hideCheckoutCustomerGroups)) {
  93.                 if (in_array($customer->getGroupId(), $config->hideCheckoutCustomerGroups)) {
  94.                     $checkoutDisableGroup true;
  95.                 }
  96.             }
  97.             $stockDisableGroup false;
  98.             if (is_array($config->hideStockCustomerGroups)) {
  99.                 if (in_array($customer->getGroupId(), $config->hideStockCustomerGroups)) {
  100.                     $stockDisableGroup true;
  101.                 }
  102.             }
  103.             if ($config->showPricesAfterLogin && !$priceDisableGroup) {
  104.                 $config->hidePrices false;
  105.             }
  106.             if ($config->enableCheckoutAfterLogin && !$checkoutDisableGroup) {
  107.                 $config->disableCheckout false;
  108.             }
  109.             if ($config->enableStockAfterLogin && !$stockDisableGroup) {
  110.                 $config->hideStock false;
  111.             }
  112.             $config->showLoginButton false;
  113.         }
  114.         if ($asArray) {
  115.             return $config->getConfig();
  116.         }
  117.         return $config;
  118.     }
  119.     /**
  120.      * @param StorefrontRenderEvent $event
  121.      */
  122.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  123.     {
  124.         $event->getContext()->addExtension(self::EXTENSION_NAME$this->getConfigData($event->getSalesChannelContext()));
  125.     }
  126.     /**
  127.      * check if checkout is disabled
  128.      *
  129.      * @param ControllerArgumentsEvent $event
  130.      */
  131.     public function onControllerEvent(ControllerArgumentsEvent $event): void
  132.     {
  133.         $controller $event->getController();
  134.         if (is_array($controller)) {
  135.             $controller $controller[0];
  136.         }
  137.         if ($controller instanceof CheckoutController) {
  138.             /** @var SalesChannelContext $context */
  139.             $context $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  140.             $config $this->getConfigData($contexttrue);
  141.             // only exist if config saved with activated setting for the first time
  142.             if (!isset($config['disableCheckout'])) {
  143.                 return;
  144.             }
  145.             // disable checkout by forwarding to home
  146.             if ($config['disableCheckout']) {
  147.                 if ($event->getRequest()->isXmlHttpRequest()) {
  148.                     $event->setController(function() {
  149.                         return new Response();
  150.                     });
  151.                 } else {
  152.                     $event->setController(function() {
  153.                         return new RedirectResponse($this->router->generate('frontend.home.page'));
  154.                     });
  155.                 }
  156.             }
  157.         }
  158.     }
  159.     /**
  160.      * assign zero prices to avoid price info in meta tag (source code)
  161.      *
  162.      * @param ProductPageLoadedEvent $event
  163.      */
  164.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  165.     {
  166.         if (!$config $this->getConfigData($event->getSalesChannelContext())) {
  167.             return;
  168.         }
  169.         if (!$config->hidePrices) {
  170.             return;
  171.         }
  172.         $page $event->getPage();
  173.         $product $page->getProduct();
  174.         $priceCollection = new PriceCollection([]);
  175.         $product->setCalculatedPrices($priceCollection);
  176.         $calculatedPrice =$product->getCalculatedPrice();
  177.         $calculatedPrice->assign(['unitPrice' => 0'totalPrice' => 0]);
  178.     }
  179. }