custom/plugins/CompraMinimumOrderValueSW6/src/Storefront/Subscriber/FrontendSubscriber.php line 85

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\MinimumOrderValueSW6\Storefront\Subscriber;
  3. use Compra\MinimumOrderValueSW6\Core\Checkout\Services\MinimumOrderService;
  4. use Psr\Container\ContainerInterface;
  5. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  6. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  7. use Shopware\Core\Checkout\Cart\Event\AfterLineItemQuantityChangedEvent;
  8. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  9. use Shopware\Core\Framework\Struct\ArrayEntity;
  10. use Shopware\Core\System\Country\CountryEntity;
  11. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class FrontendSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var ContainerInterface  */
  18.     protected $container;
  19.     /** @var CartService */
  20.     protected $cartService;
  21.     /** @var MinimumOrderService */
  22.     protected $minimumOrderService;
  23.     public function __construct
  24.     (
  25.         ContainerInterface $container,
  26.         CartService $cartService,
  27.         MinimumOrderService $minimumOrderService
  28.     )
  29.     {
  30.         $this->container $container;
  31.         $this->cartService $cartService;
  32.         $this->minimumOrderService $minimumOrderService;
  33.     }
  34.     /**
  35.      * @return array
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             CheckoutConfirmPageLoadedEvent::class => 'addCartExtension',
  41.             CheckoutCartPageLoadedEvent::class => 'addCartExtension',
  42.             OffcanvasCartPageLoadedEvent::class => 'addCartExtension',
  43.             AfterLineItemAddedEvent::class => 'onCartUpdated',
  44.             AfterLineItemQuantityChangedEvent::class => 'onCartUpdated'
  45.         ];
  46.     }
  47.     /**
  48.      * Removes the cart error when the cart is updated
  49.      * @param AfterLineItemAddedEvent|AfterLineItemQuantityChangedEvent $event
  50.      */
  51.     public function onCartUpdated($event)
  52.     {
  53.         $salesChannelContext $event->getSalesChannelContext();
  54.         $this->minimumOrderService->init($salesChannelContext);
  55.         $cart $event->getCart();
  56.         $country $this->minimumOrderService->getShippingCountry();
  57.         $minimumOrderValue $this->minimumOrderService->checkMinimumOrderValueForCountry($salesChannelContext$cart$country);
  58.         if (!$minimumOrderValue || $minimumOrderValue <= $cart->getPrice()->getPositionPrice()) {
  59.             /** @var ErrorCollection $cartErrors */
  60.             $cartErrors $cart->getErrors();
  61.             if ($cartErrors->get('minimum-order-value-not-reached') !== null) {
  62.                 $cartErrors->remove('minimum-order-value-not-reached');
  63.             }
  64.         }
  65.     }
  66.     /**
  67.      * Adds an extension to the frontend to allow alert configuration
  68.      * @param CheckoutConfirmPageLoadedEvent|CheckoutCartPageLoadedEvent|OffcanvasCartPageLoadedEvent $event
  69.      */
  70.     public function addCartExtension($event): void
  71.     {
  72.         $salesChannelContext $event->getSalesChannelContext();
  73.         $salesChannelId $salesChannelContext->getSalesChannelId();
  74.         // Initializes minimum order service to make all the variables accessible
  75.         $this->minimumOrderService->init($salesChannelContext);
  76.         $cart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  77.         if
  78.         (
  79.             !$this->minimumOrderService->getConfigData('activateInThisShop'$salesChannelId)
  80.             || !$cart->getLineItems()->getElements()
  81.         )
  82.         {
  83.             // Don't do anything if functionality is disabled for this (sub)shop or if the cart is empty.
  84.             return;
  85.         }
  86.         // Gets shipping country
  87.         /** @var CountryEntity $country */
  88.         $country $this->minimumOrderService->getShippingCountry();
  89.         /**
  90.          * Checks if minimum order value is met
  91.          * @var float|false $minimumOrderValue
  92.          */
  93.         $minimumOrderValue $this->minimumOrderService->checkMinimumOrderValueForCountry($salesChannelContext$cart$country);
  94.         // Displays an error and blocks the order if the value is not met.
  95.         if ($minimumOrderValue) {
  96.             $languageId $salesChannelContext->getLanguageId();
  97.             // Formats minimum order value as currency
  98.             $minimumOrderValue $this->minimumOrderService->formatMinimumValue($minimumOrderValue$languageId);
  99.             // Gets message type from plugin config.
  100.             $messageType $this->minimumOrderService->getConfigData('messageType'$salesChannelId);
  101.             $extensionData = [
  102.                 'minimumOrderValue' => $minimumOrderValue,
  103.                 'messageType' => $messageType
  104.             ];
  105.             // Adds extension to cart
  106.             $cart->addExtension('compraMinimumOrderValue', new ArrayEntity($extensionData));
  107.         }
  108.     }
  109. }