custom/plugins/PremsIndividualOffer6/src/Subscriber/CheckoutPageLoader.php line 34

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