custom/plugins/WbfkOffer/src/Subscriber/RestrictPaymentMethodsForOffer.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\Offer\Subscriber;
  4. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPage;
  5. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
  7. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  12. use Wbfk\Offer\Core\Offer\Storefront\OfferService;
  13. use Wbfk\Offer\Service\OfferLineItemHandler;
  14. class RestrictPaymentMethodsForOffer implements EventSubscriberInterface
  15. {
  16.     public function __construct(private OfferService $offerService)
  17.     {
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CheckoutCartPageLoadedEvent::class => 'filterPaymentMethodsForOfferCart',
  23.             CheckoutConfirmPageLoadedEvent::class => 'filterPaymentMethodsForOfferCart',
  24.             AccountEditOrderPageLoadedEvent::class => 'filterPaymentMethodsForOfferOrder',
  25.         ];
  26.     }
  27.     public function filterPaymentMethodsForOfferCart(CheckoutCartPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event): void
  28.     {
  29.         $page $event->getPage();
  30.         $cart $page->getCart();
  31.         $offerLineItems $cart->getLineItems()->filterFlatByType(OfferLineItemHandler::TYPE_OFFER);
  32.         if (empty($offerLineItems)) {
  33.             return;
  34.         }
  35.         foreach ($offerLineItems as $offerLineItem) {
  36.             $offerId $offerLineItem->getReferencedId();
  37.             $data $cart->getData();
  38.             if (!$data->has($this->buildKey($offerId))) {
  39.                 continue;
  40.             }
  41.             $offer $data->get($this->buildKey($offerId));
  42.             $this->filterPaymentMethodsForOffer($offer$page);
  43.         }
  44.     }
  45.     public function filterPaymentMethodsForOfferOrder(AccountEditOrderPageLoadedEvent $event): void
  46.     {
  47.         $page $event->getPage();
  48.         $order $page->getOrder();
  49.         $offerLineItems $order->getLineItems()->filterByType(OfferLineItemHandler::TYPE_OFFER)->getElements();
  50.         if (empty($offerLineItems)) {
  51.             return;
  52.         }
  53.         foreach ($offerLineItems as $offerLineItem) {
  54.             $offerId $offerLineItem->getReferencedId();
  55.             $offer $this->offerService->getOfferById($offerId$event->getContext());
  56.             if (!$offer) {
  57.                 continue;
  58.             }
  59.             $this->filterPaymentMethodsForOffer($offer$page);
  60.         }
  61.     }
  62.     private function buildKey(string $id): string
  63.     {
  64.         return 'wbfk-offer-' $id;
  65.     }
  66.     public function filterPaymentMethodsForOffer(OfferEntity $offerCheckoutConfirmPage|CheckoutCartPage|AccountEditOrderPage $page): void
  67.     {
  68.         $offerPaymentMethods $offer->getPaymentMethods();
  69.         if (empty($offerPaymentMethods)) {
  70.             return;
  71.         }
  72.         $filtered $page->getPaymentMethods()->filter(function ($paymentMethod) use ($offerPaymentMethods) {
  73.             return in_array($paymentMethod->getId(), $offerPaymentMethods);
  74.         });
  75.         $page->setPaymentMethods($filtered);
  76.     }
  77. }