custom/plugins/CoeHidePriceForCustomerGroup/src/Subscriber/PageLoadSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace CoeHidePriceForCustomerGroup\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Storefront\Event\StorefrontRenderEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. /**
  10.  * Class PageLoadSubscriber
  11.  * @package CoeHidePriceForCustomerGroup\Subscriber
  12.  * @author Jeffry Block <jeffry.block@codeenterprise.de>
  13.  */
  14. class PageLoadSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var string */
  17.     public const PARAM_HIDE_PRICE "coeHidePrice";
  18.     /** @var string */
  19.     public const PARAM_DISABLE_CHECKOUT "coeHideCheckoutButtons";
  20.     /** @var string */
  21.     public const PARAM_HIDE_ADD_TO_CART "coeHideAddToCartButtons";
  22.     /** @var bool */
  23.     public const PARAM_SHOW_CART_REPLACEMENT "coeShowAddToCartTextReplace";
  24.     public const PARAM_SHOW_CART_REDIRECT "coeShowAddToCartRedirect";
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $configService;
  29.     /**
  30.      * PageLoadSubscriber constructor.
  31.      * @param SystemConfigService $configService
  32.      */
  33.     public function __construct(SystemConfigService $configService)
  34.     {
  35.         $this->configService $configService;
  36.     }
  37.     /**
  38.      * @return array
  39.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             StorefrontRenderEvent::class => "onRenderStorefront"
  45.         ];
  46.     }
  47.     /**
  48.      * @param StorefrontRenderEvent $event
  49.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  50.      */
  51.     public function onRenderStorefront(StorefrontRenderEvent $event){
  52.         /** @var SalesChannelContext $context */
  53.         $context $event->getSalesChannelContext();
  54.         /** @var CustomerGroupEntity $customerGroup */
  55.         $customerGroup $context->getCurrentCustomerGroup();
  56.         /** @var array $customFields */
  57.         $customFields $customerGroup->getCustomFields();
  58.         /** @var CustomerEntity $customer */
  59.         $customer $context->getCustomer();
  60.         if(is_null($customer) || $customer->getGuest()){
  61.             $hidePrice $this->configService->get(
  62.                 "CoeHidePriceForCustomerGroup.config.coeHidePriceForGuests",
  63.                 $context->getSalesChannel()->getId()
  64.             );
  65.         }else if(!is_array($customFields) || !isset($customFields[PageLoadSubscriber::PARAM_HIDE_PRICE])){
  66.             $hidePrice false;
  67.         }else{
  68.             $hidePrice $customFields[PageLoadSubscriber::PARAM_HIDE_PRICE] == "1";
  69.         }
  70.         $hideCheckoutButton $hidePrice $this->configService->get(
  71.             "CoeHidePriceForCustomerGroup.config.coeHideCheckoutButtons",
  72.             $context->getSalesChannel()->getId()) : false;
  73.         $hideAddToCartButtons $hidePrice $this->configService->get(
  74.             "CoeHidePriceForCustomerGroup.config.coeHideAddToCartButtons",
  75.             $context->getSalesChannel()->getId()) : false;
  76.         $showCartTextReplacement $hidePrice $this->configService->get(
  77.             "CoeHidePriceForCustomerGroup.config.coeShowAddToCartTextReplace",
  78.             $context->getSalesChannel()->getId()) : false;
  79.         $showCartRedirect $hidePrice $this->configService->get(
  80.             "CoeHidePriceForCustomerGroup.config.coeShowAddToCartRedirect",
  81.             $context->getSalesChannel()->getId()) : null;
  82.         $event->setParameter(PageLoadSubscriber::PARAM_HIDE_PRICE$hidePrice);
  83.         $event->setParameter(PageLoadSubscriber::PARAM_HIDE_ADD_TO_CART$hideAddToCartButtons);
  84.         $event->setParameter(PageLoadSubscriber::PARAM_DISABLE_CHECKOUT$hideCheckoutButton);
  85.         $event->setParameter(PageLoadSubscriber::PARAM_SHOW_CART_REPLACEMENT$showCartTextReplacement);
  86.         $event->setParameter(PageLoadSubscriber::PARAM_SHOW_CART_REDIRECT$showCartRedirect);
  87.     }
  88. }