<?php declare(strict_types=1);
namespace NetInventors\NetiNextFreeDelivery\Subscriber;
use NetInventors\NetiNextFreeDelivery\Components\ShippingFreeService;
use NetInventors\NetiNextFreeDelivery\Service\PluginConfig;
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
{
private PluginConfig $pluginConfig;
private ShippingFreeService $shippingFreeService;
public function __construct(
PluginConfig $pluginConfig,
ShippingFreeService $shippingFreeService
) {
$this->pluginConfig = $pluginConfig;
$this->shippingFreeService = $shippingFreeService;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductLoaded',
CheckoutCartPageLoadedEvent::class => 'onCheckoutPageLoaded',
OffcanvasCartPageLoadedEvent::class => 'onAjaxCartPageLoaded',
HeaderPageletLoadedEvent::class => 'headerLoadedEvent',
];
}
/**
* @param HeaderPageletLoadedEvent $event
*/
public function headerLoadedEvent(HeaderPageletLoadedEvent $event): void
{
$isActivated = $this->pluginConfig->isSubshopActivated();
$show = $this->pluginConfig->isShowInHeader();
if (!$isActivated || !$show) {
return;
}
$event->getPagelet()->addExtension(
'netiNextFreeDelivery',
$this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
);
}
/**
* @param OffcanvasCartPageLoadedEvent $event
*/
public function onAjaxCartPageLoaded(OffcanvasCartPageLoadedEvent $event): void
{
$isActivated = $this->pluginConfig->isSubshopActivated();
$show = $this->pluginConfig->isShowInModal();
if (!$isActivated || !$show) {
return;
}
$event->getPage()->assign(
[
'netiNextFreeDelivery' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
]
);
}
/**
* @param CheckoutCartPageLoadedEvent $event
*/
public function onCheckoutPageLoaded(CheckoutCartPageLoadedEvent $event): void
{
$isActivated = $this->pluginConfig->isSubshopActivated();
if (!$isActivated) {
return;
}
$event->getPage()->assign(
[
'netiNextFreeDelivery' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
]
);
}
/**
* @param ProductPageLoadedEvent $event
*/
public function onProductLoaded(ProductPageLoadedEvent $event): void
{
$isActivated = $this->pluginConfig->isSubshopActivated();
$show = $this->pluginConfig->isShowInArticle();
if (!$isActivated || !$show) {
return;
}
$event->getPage()->assign(
[
'netiNextFreeDelivery' => $this->shippingFreeService->calculateShippingCostsDifference(
$event->getSalesChannelContext()
),
]
);
}
}