custom/plugins/NextagCheckout/src/Subscriber/PaymentMethodSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace Nextag\Checkout\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  4. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  8. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  9. class PaymentMethodSubscriber implements EventSubscriberInterface
  10. {
  11.     private EntityRepository $productRepository;
  12.     private SalesChannelContextPersister $contextPersister;
  13.     public function __construct(EntityRepository $productRepositorySalesChannelContextPersister $contextPersister)
  14.     {
  15.         $this->productRepository $productRepository;
  16.         $this->contextPersister $contextPersister;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmPageLoaded',
  22.         ];
  23.     }
  24.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  25.     {
  26.         $page $event->getPage();
  27.         $methods $page->getPaymentMethods();
  28.         $cart $event->getPage()->getCart(); // ✅ correct for confirm page
  29.         $salesChannelContext $event->getSalesChannelContext();
  30.         $context $event->getSalesChannelContext()->getContext();
  31.         $customerGroup strtolower($event->getSalesChannelContext()->getCurrentCustomerGroup()?->getTranslated()['name'] ?? '');
  32.         $exclude false;
  33.         foreach ($cart->getLineItems() as $item) {
  34.             $productId $item->getReferencedId();
  35.     
  36.             $criteria = new Criteria([$productId]);
  37.             $product $this->productRepository->search($criteria$context)->first();
  38.             if (!$product) {
  39.                 continue;
  40.             }
  41.             $customFields $product->getCustomFields() ?? [];
  42.             $isZuteilungswein $customFields['custom_produkt_label_produkt_zuteilungswein'] ?? false;
  43.             if (
  44.                 $isZuteilungswein
  45.             ) {
  46.                 $exclude true;
  47.                 break;
  48.             }
  49.     
  50.             /* if (
  51.                 ($customerGroup === 'privat' && ($customFields['custom_produkt_label_produkt_aktionsartikel_pr_specialsellingvisible'] ?? null) === '1') ||
  52.                 ($customerGroup === 'wiederverkauf' && ($customFields['custom_produkt_label_produkt_aktionsartikel_ga_specialsellingvisible'] ?? null) === '1') ||
  53.                 ($customerGroup === 'mitarbeiter' && ($customFields['custom_produkt_label_produkt_aktionsartikel_mi_specialsellingvisible'] ?? null) === '1') ||
  54.                 ($customerGroup === 'privat' && ($customFields['custom_produkt_label_produkt_aktionsartikel_pr_specialsellingvisible'] ?? null) === '2') ||
  55.                 ($customerGroup === 'wiederverkauf' && ($customFields['custom_produkt_label_produkt_aktionsartikel_ga_specialsellingvisible'] ?? null) === '3') ||
  56.                 ($customerGroup === 'mitarbeiter' && ($customFields['custom_produkt_label_produkt_aktionsartikel_mi_specialsellingvisible'] ?? null) === '4')
  57.             ) {
  58.                 $exclude = true;
  59.                 break;
  60.             } */
  61.         }
  62.         if ($exclude) {
  63.             // Filter only "invoice_payment"
  64.             $filtered $methods->filter(function ($method) {
  65.                 return $method->getShortName() === 'invoice_payment';
  66.             });
  67.         
  68.             $page->setPaymentMethods($filtered);
  69.             // Check if selected method is no longer in the filtered list
  70.             $currentPayment $salesChannelContext->getPaymentMethod();
  71.             $currentPaymentId $currentPayment $currentPayment->getId() : null;
  72.             $isCurrentStillAvailable $filtered->has($currentPaymentId);
  73.             if (!$isCurrentStillAvailable && $filtered->count() > 0) {
  74.                 $newPaymentMethod $filtered->first();
  75.                 $salesChannelContext->assign(['paymentMethod' => $newPaymentMethod]);
  76.                 
  77.                 $token $salesChannelContext->getToken();
  78.                 $persisterContext $salesChannelContext->getContext();
  79.                 $salesChannelId $salesChannelContext->getSalesChannel()->getId();
  80.                 $this->contextPersister->save(
  81.                     $token,
  82.                     [SalesChannelContextService::PAYMENT_METHOD_ID => $newPaymentMethod->getId()],
  83.                     $salesChannelId
  84.                 );
  85.                 
  86.             }
  87.         }
  88.     }
  89. }