custom/plugins/AvencyShopwareCore/src/Core/Subscriber/ProductPageSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Avency\Shopware\Core\Core\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ProductPageSubscriber implements EventSubscriberInterface
  8. {
  9.     const SNIPPET_PRODUCT_ALLWAYS 'avencyMinimumOrderValue.product';
  10.     const SNIPPET_PRODUCT_EXCLUDE 'avencyMinimumOrderValue.productExclude';
  11.     const SNIPPET_PRODUCT_EXCLUDE_WITH_VALUE 'avencyMinimumOrderValue.productExcludeWithValue';
  12.     const SNIPPET_PRODUCT_INCLUDE 'avencyMinimumOrderValue.productInclude';
  13.     const SNIPPET_PRODUCT_INCLUDE_WITH_VALUE 'avencyMinimumOrderValue.productIncludeWithValue';
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     protected $systemConfigService;
  18.     public function __construct(SystemConfigService $systemConfigService)
  19.     {
  20.         $this->systemConfigService $systemConfigService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             ProductPageLoadedEvent::class => 'extendProductPage',
  26.         ];
  27.     }
  28.     /**
  29.      * @param ProductPageLoadedEvent $event
  30.      */
  31.     public function extendProductPage(ProductPageLoadedEvent $event)
  32.     {
  33.         $config $this->systemConfigService->get('AvencyShopwareCore.config'$event->getSalesChannelContext()->getSalesChannelId());
  34.         if ($snippetName $this->getSnippetName($event$config)) {
  35.             $event->getPage()->getProduct()->addExtension('avencyMinimumOrderProduct', new ArrayStruct([
  36.                 'snippetName' => $snippetName,
  37.                 'minimumOrderAmount' => $config['avencyMinimumOrderAmount'],
  38.             ]));
  39.         }
  40.     }
  41.     /**
  42.      * Get the snippet name
  43.      *
  44.      * @param ProductPageLoadedEvent $event
  45.      * @param array $config
  46.      * @return string
  47.      */
  48.     private function getSnippetName(ProductPageLoadedEvent $event, array $config): ?string
  49.     {
  50.         $snippetName null;
  51.         $minimumOrderValueDefinedByRule = isset($config['avencyMinimumOrderRuleId']) && $config['avencyMinimumOrderExtendedAmount'];
  52.         if (($config['avencyMinimumOrderProductMessage'] ?? false) && !$minimumOrderValueDefinedByRule) {
  53.             $snippetName self::SNIPPET_PRODUCT_ALLWAYS;
  54.         }
  55.         if ($config['avencyMinimumOrderIncExcMessage'] ?? false) {
  56.             if (in_array($event->getPage()->getProduct()->getId(), $config['avencyMinimumOrderExcludeProductIds'] ?? [])) {
  57.                 if ($minimumOrderValueDefinedByRule) {
  58.                     $snippetName $config['avencyMinimumOrderIncExcProductIds'] ?? false self::SNIPPET_PRODUCT_INCLUDE self::SNIPPET_PRODUCT_EXCLUDE;
  59.                 } else {
  60.                     $snippetName $config['avencyMinimumOrderIncExcProductIds'] ?? false self::SNIPPET_PRODUCT_INCLUDE_WITH_VALUE self::SNIPPET_PRODUCT_EXCLUDE_WITH_VALUE;
  61.                 }
  62.             }
  63.         }
  64.         return $snippetName;
  65.     }
  66. }