custom/plugins/DigaMinimumOrderValue/src/Subscriber/CheckoutPageSubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DigaMinimumOrderValue\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Event\StorefrontRenderEvent;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  10. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPage;
  11. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPage;
  12. use Shopware\Storefront\Page\Page;
  13. class CheckoutPageSubscriber implements EventSubscriberInterface {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $configService;
  18.     /**
  19.      * @var LoggerInterface
  20.      */
  21.     private $logger;
  22.     
  23.     public function __construct(SystemConfigService $systemConfigServiceLoggerInterface $logger) {
  24.         $this->configService $systemConfigService;
  25.         $this->logger $logger;
  26.     }
  27.     public static function getSubscribedEvents(): array {
  28.         return [
  29.             StorefrontRenderEvent::class => 'onStorfrontRender'
  30.         ];
  31.     }
  32.     /**
  33.      *
  34.      * @param StorefrontRenderEvent $event
  35.      * @return void
  36.      */
  37.     public function onStorfrontRender(StorefrontRenderEvent $event): void {
  38.         try {
  39.             if(!isset($event->getParameters()['page'])) {
  40.                 return;
  41.             }
  42.             /** @var Page $currentPage */
  43.             $currentPage $event->getParameters()['page'];
  44.             if (($currentPage instanceof CheckoutCartPage) or 
  45.                 ($currentPage instanceof OffcanvasCartPage) or 
  46.                 ($currentPage instanceof CheckoutConfirmPage) or
  47.                 ($currentPage instanceof CheckoutRegisterPage)){
  48.                     $this->onCheckoutPageLoaded($event);
  49.             }
  50.             
  51.         }catch (\Exception $e) { 
  52.             $this->logger->log('error',"DigaCheckoutPageSubscriber: error : ".$e);
  53.         }
  54.     }
  55.     /**
  56.      *
  57.      * @param StorefrontRenderEvent $event
  58.      * @return void
  59.      */
  60.     public function onCheckoutPageLoaded(StorefrontRenderEvent $event): void {
  61.         try {
  62.             //$this->logger->debug("DigaCheckoutPageSubscriber startet");
  63.             // first check if plugin active and has config on saleschannel
  64.             $salesChannelId$event->getSalesChannelContext()->getSalesChannelId();
  65.             $pluginActive $this->configService->get('DigaMinimumOrderValue.config.pluginactive'$salesChannelId);
  66.             if($pluginActive){
  67.                 $excluded false;
  68.                 $disableButton false;
  69.                 $priceSource $this->configService->get('DigaMinimumOrderValue.config.digaminordertotal'$salesChannelId);
  70.                 $minOrderValPrice $event->getParameters()['page']->getCart()->getPrice()->getPositionPrice(); 
  71.                 if ($priceSource == "netPrice" ){
  72.                     $minOrderValPrice $event->getParameters()['page']->getCart()->getPrice()->getNetPrice(); 
  73.                 }elseif($priceSource == "totalPrice"){
  74.                     $minOrderValPrice $event->getParameters()['page']->getCart()->getPrice()->getTotalPrice();
  75.                 }
  76.                 if ( ($this->configService->get('DigaMinimumOrderValue.config.digaminorderval'$salesChannelId)) && ($minOrderValPrice !== null) ){
  77.                     //exclude by group
  78.                     $currentCustomerGroupId $event->getSalesChannelContext()->getCurrentCustomerGroup()->getId();
  79.                     /** @var mixed $groupsFromConfig */
  80.                     $groupsFromConfig $this->configService->get('DigaMinimumOrderValue.config.excludegroupfromminorderval'$salesChannelId);
  81.                     if ($groupsFromConfig != null && is_array($groupsFromConfig)){
  82.                         if (in_array($currentCustomerGroupId$groupsFromConfig)) {
  83.                             $excluded true;
  84.                         }
  85.                     }
  86.                     //exclude by rule
  87.                     $ruleIds $event->getSalesChannelContext()->getRuleIds();
  88.                     $ruleIdFromConfig $this->configService->get('DigaMinimumOrderValue.config.excludebyrulefromminorderval'$salesChannelId);
  89.                     if (in_array($ruleIdFromConfig$ruleIds)) {
  90.                         $excluded true;
  91.                     }
  92.                     //show info: ?add cart error
  93.                     if( !$excluded){
  94.                         $minValueFromConfig=$this->configService->get('DigaMinimumOrderValue.config.digaminorderval'$salesChannelId);
  95.                         if( ($minValueFromConfig $minOrderValPrice) && ($minOrderValPrice !== null) ){
  96.                             $disableButtontrue;
  97.                         }
  98.                     }
  99.                 }
  100.                 $digaMinValueData=["disableButton"=>$disableButton];
  101.                 //add the array to the context as an extension
  102.                 $event->getContext()->addExtension('digaMinValueData', new ArrayEntity($digaMinValueData));
  103.             }
  104.         }catch (\Exception $e) { 
  105.             $this->logger->log('error',"DigaCheckoutPageSubscriber: error : ".$e);
  106.         }
  107.     }
  108. }