<?phpdeclare(strict_types=1);namespace Imnxx\Subscriber;use Imnxx\Services\SepaService;use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class CheckoutSubscriber implements EventSubscriberInterface{ public function __construct(private readonly SepaService $sepaService) {} public static function getSubscribedEvents(): array { return [ CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded', CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded', ]; } public function onCheckoutFinishLoaded($event): void { $orderHasNeo = false; $neoLineItemId = ''; $lastAddedLineItemId = ''; $order = $event->getPage()->getOrder(); $lineItems = $order->getLineItems(); foreach ($lineItems as $lineItem) { $payload = $lineItem->getPayload(); if ($payload['productNumber'] != null && str_contains($payload['productNumber'], 'NEO')) { $orderHasNeo = true; } } if ($orderHasNeo) { $event->getPage()->assign( [ 'hideShippingAddress' => true, ] ); } } public function onCheckoutConfirmLoaded($event): void { $this->hideDeliveryAdress($event); $this->handleSepaLogic($event); } public function hideDeliveryAdress(CheckoutConfirmPageLoadedEvent $event): void { $cartHasNeo = false; $neoLineItemId = ''; $lastAddedLineItemId = ''; $cart = $event->getPage()->getCart(); $flatItems = $cart->getLineItems()->getFlat(); foreach ($flatItems as $lineItem) { $payload = $lineItem->getPayload(); if ($payload['productNumber'] != null && str_contains($payload['productNumber'], 'NEO')) { $cartHasNeo = true; } } if ($cartHasNeo) { $event->getPage()->assign( [ 'hideShippingAddress' => true, ] ); } } private function handleSepaLogic(CheckoutConfirmPageLoadedEvent $event): void { $customer = $event->getSalesChannelContext()->getCustomer(); if ($customer === null) { return; } $activeBillingAddress = $customer->getActiveBillingAddress(); if ($activeBillingAddress === null) { return; } $addressCustomFields = $activeBillingAddress->getCustomFields() ?? []; $customerCustomFields = $customer->getCustomFields() ?? []; $nwbAddressId = $addressCustomFields['custom_address_attributs_nwb_address_id'] ?? null; $nwbAccountId = $customerCustomFields['custom_customer_attributes_account_id'] ?? null; if ($nwbAddressId === null || $nwbAccountId === null) { return; } $activeBisAddress = $this->sepaService ->getCurrentBillingAddressInformation($nwbAddressId, $nwbAccountId); if ($activeBisAddress === null) { return; } $iban = $activeBisAddress->getIban(); $accountHolder = $activeBisAddress->getAccountHolder(); if ($iban === null || $accountHolder === null) { return; } $anonymisedIban = $this->sepaService->anonymiseIban($iban); $event->getPage()->assign([ 'imnxxSepaInfos' => [ 'iban' => $anonymisedIban, 'accountHolder' => $accountHolder ] ]); }}