<?php declare(strict_types=1);
namespace Compra\MinimumOrderValueSW6\Storefront\Subscriber;
use Compra\MinimumOrderValueSW6\Core\Checkout\Services\MinimumOrderService;
use Psr\Container\ContainerInterface;
use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemQuantityChangedEvent;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\Country\CountryEntity;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FrontendSubscriber implements EventSubscriberInterface
{
/** @var ContainerInterface */
protected $container;
/** @var CartService */
protected $cartService;
/** @var MinimumOrderService */
protected $minimumOrderService;
public function __construct
(
ContainerInterface $container,
CartService $cartService,
MinimumOrderService $minimumOrderService
)
{
$this->container = $container;
$this->cartService = $cartService;
$this->minimumOrderService = $minimumOrderService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
CheckoutConfirmPageLoadedEvent::class => 'addCartExtension',
CheckoutCartPageLoadedEvent::class => 'addCartExtension',
OffcanvasCartPageLoadedEvent::class => 'addCartExtension',
AfterLineItemAddedEvent::class => 'onCartUpdated',
AfterLineItemQuantityChangedEvent::class => 'onCartUpdated'
];
}
/**
* Removes the cart error when the cart is updated
* @param AfterLineItemAddedEvent|AfterLineItemQuantityChangedEvent $event
*/
public function onCartUpdated($event)
{
$salesChannelContext = $event->getSalesChannelContext();
$this->minimumOrderService->init($salesChannelContext);
$cart = $event->getCart();
$country = $this->minimumOrderService->getShippingCountry();
$minimumOrderValue = $this->minimumOrderService->checkMinimumOrderValueForCountry($salesChannelContext, $cart, $country);
if (!$minimumOrderValue || $minimumOrderValue <= $cart->getPrice()->getPositionPrice()) {
/** @var ErrorCollection $cartErrors */
$cartErrors = $cart->getErrors();
if ($cartErrors->get('minimum-order-value-not-reached') !== null) {
$cartErrors->remove('minimum-order-value-not-reached');
}
}
}
/**
* Adds an extension to the frontend to allow alert configuration
* @param CheckoutConfirmPageLoadedEvent|CheckoutCartPageLoadedEvent|OffcanvasCartPageLoadedEvent $event
*/
public function addCartExtension($event): void
{
$salesChannelContext = $event->getSalesChannelContext();
$salesChannelId = $salesChannelContext->getSalesChannelId();
// Initializes minimum order service to make all the variables accessible
$this->minimumOrderService->init($salesChannelContext);
$cart = $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
if
(
!$this->minimumOrderService->getConfigData('activateInThisShop', $salesChannelId)
|| !$cart->getLineItems()->getElements()
)
{
// Don't do anything if functionality is disabled for this (sub)shop or if the cart is empty.
return;
}
// Gets shipping country
/** @var CountryEntity $country */
$country = $this->minimumOrderService->getShippingCountry();
/**
* Checks if minimum order value is met
* @var float|false $minimumOrderValue
*/
$minimumOrderValue = $this->minimumOrderService->checkMinimumOrderValueForCountry($salesChannelContext, $cart, $country);
// Displays an error and blocks the order if the value is not met.
if ($minimumOrderValue) {
$languageId = $salesChannelContext->getLanguageId();
// Formats minimum order value as currency
$minimumOrderValue = $this->minimumOrderService->formatMinimumValue($minimumOrderValue, $languageId);
// Gets message type from plugin config.
$messageType = $this->minimumOrderService->getConfigData('messageType', $salesChannelId);
$extensionData = [
'minimumOrderValue' => $minimumOrderValue,
'messageType' => $messageType
];
// Adds extension to cart
$cart->addExtension('compraMinimumOrderValue', new ArrayEntity($extensionData));
}
}
}