<?php declare(strict_types=1);
namespace ASEServices\ASENextFreeDelivery\Subscriber;
use Doctrine\DBAL\DBALException;
use ASEServices\ASENextFreeDelivery\Components\ShippingFreeService;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Frontend implements EventSubscriberInterface
{
/**
* @var array
*/
protected $config;
/**
* @var ShippingFreeService
*/
private $shippingFreeService;
public function __construct(
SystemConfigService $configService,
ShippingFreeService $shippingFreeService
) {
$this->config = $configService->getDomain('ASENextFreeDelivery');
$this->shippingFreeService = $shippingFreeService;
}
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => 'onProductLoaded',
CheckoutCartPageLoadedEvent::class => 'onCheckoutPageLoaded',
OffcanvasCartPageLoadedEvent::class => 'onAjaxCartPageLoaded',
HeaderPageletLoadedEvent::class => 'headerLoadedEvent',
];
}
/**
* @param HeaderPageletLoadedEvent $event
*
* @throws DBALException
*/
public function headerLoadedEvent(HeaderPageletLoadedEvent $event): void
{
$isActivated = $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
$show = $this->config['ASENextFreeDelivery.config.showInHeader'] ?? false;
if (!$isActivated || !$show) {
return;
}
$event->getPagelet()->setExtensions([
'aseNextFreeDelivery' => [
'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
)
]
]);
}
/**
* @param OffcanvasCartPageLoadedEvent $event
*
* @throws DBALException
*/
public function onAjaxCartPageLoaded(OffcanvasCartPageLoadedEvent $event): void
{
$isActivated = $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
$show = $this->config['ASENextFreeDelivery.config.showInModal'] ?? false;
if (!$isActivated || !$show) {
return;
}
$event->getPage()->assign(
[
'aseNextFreeDelivery' => [
'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
],
]
);
}
/**
* @param CheckoutCartPageLoadedEvent $event
*
* @throws DBALException
*/
public function onCheckoutPageLoaded(CheckoutCartPageLoadedEvent $event): void
{
$isActivated = $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
if (!$isActivated) {
return;
}
$event->getPage()->assign(
[
'aseNextFreeDelivery' => [
'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
],
]
);
}
/**
* @param ProductPageLoadedEvent $event
*
* @throws DBALException
*/
public function onProductLoaded(ProductPageLoadedEvent $event): void
{
$isActivated = $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
$show = $this->config['ASENextFreeDelivery.config.showInArticle'] ?? false;
if (!$isActivated || !$show) {
return;
}
$event->getPage()->assign(
[
'aseNextFreeDelivery' => [
'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
],
]
);
}
}