<?php
namespace Lnb\Shopware6\CheckoutCustomization\Subscriber;
use Lnb\Shopware6\CheckoutCustomization\Content\Savings\SavingsCalculator;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartSavingsSubscriber implements EventSubscriberInterface
{
private SavingsCalculator $savingsCalculator;
private CartService $cartService;
public function __construct(SavingsCalculator $savingsCalculator, CartService $cartService)
{
$this->savingsCalculator = $savingsCalculator;
$this->cartService = $cartService;
}
public static function getSubscribedEvents(): array
{
return [
SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolvedEvent',
];
}
public function onSalesChannelContextResolvedEvent(SalesChannelContextResolvedEvent $event): void
{
$cart = $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
$this->savingsCalculator->calculate($cart);
}
}