custom/plugins/WbfkOffer/src/Subscriber/CheckoutPageLoader.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\Offer\Subscriber;
  4. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  5. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  9. use Wbfk\Offer\Core\Offer\Storefront\OfferService;
  10. class CheckoutPageLoader implements EventSubscriberInterface
  11. {
  12.     private OfferService $offerService;
  13.     public function __construct(OfferService $offerService)
  14.     {
  15.         $this->offerService $offerService;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutLoaded',
  21.             CheckoutCartPageLoadedEvent::class => 'onCheckoutLoaded',
  22.             OffcanvasCartPageLoadedEvent::class => 'onCheckoutLoaded'
  23.         ];
  24.     }
  25.     public function onCheckoutLoaded(CheckoutConfirmPageLoadedEvent|CheckoutCartPageLoadedEvent|OffcanvasCartPageLoadedEvent $event): void
  26.     {
  27.         // ToDo: Reduce to available Shipping methods
  28.         /** @var OfferEntity $offer * /
  29.          * $offer = $this->offerService->getOfferById($offerExtension->getId(), $event->getSalesChannelContext()->getContext(), $customer);
  30.          * $shippingMethodIds = $offer->getShippingMethods();
  31.          * if (!$shippingMethodIds) {
  32.          * return;
  33.          * }
  34.          * foreach ($page->getShippingMethods() as $key => $shippingMethod) {
  35.          * if (!in_array($shippingMethod->getId(), $shippingMethodIds)) {
  36.          * $page->getShippingMethods()->remove($key);
  37.          * }
  38.          * }
  39.          * /* */
  40.     }
  41. }