<?php
declare(strict_types=1);
namespace Wbfk\Offer\Subscriber;
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;
use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
use Wbfk\Offer\Core\Offer\Storefront\OfferService;
class CheckoutPageLoader implements EventSubscriberInterface
{
private OfferService $offerService;
public function __construct(OfferService $offerService)
{
$this->offerService = $offerService;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutLoaded',
CheckoutCartPageLoadedEvent::class => 'onCheckoutLoaded',
OffcanvasCartPageLoadedEvent::class => 'onCheckoutLoaded'
];
}
public function onCheckoutLoaded(CheckoutConfirmPageLoadedEvent|CheckoutCartPageLoadedEvent|OffcanvasCartPageLoadedEvent $event): void
{
// ToDo: Reduce to available Shipping methods
/** @var OfferEntity $offer * /
* $offer = $this->offerService->getOfferById($offerExtension->getId(), $event->getSalesChannelContext()->getContext(), $customer);
* $shippingMethodIds = $offer->getShippingMethods();
* if (!$shippingMethodIds) {
* return;
* }
* foreach ($page->getShippingMethods() as $key => $shippingMethod) {
* if (!in_array($shippingMethod->getId(), $shippingMethodIds)) {
* $page->getShippingMethods()->remove($key);
* }
* }
* /* */
}
}