custom/plugins/NwbNeo/src/Subscriber/CheckoutSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Imnxx\Subscriber;
  4. use Imnxx\Services\SepaService;
  5. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CheckoutSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private readonly SepaService $sepaService) {}
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  15.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded',
  16.         ];
  17.     }
  18.     public function onCheckoutFinishLoaded($event): void
  19.     {
  20.         $orderHasNeo false;
  21.         $neoLineItemId '';
  22.         $lastAddedLineItemId '';
  23.         $order $event->getPage()->getOrder();
  24.         $lineItems $order->getLineItems();
  25.         foreach ($lineItems as $lineItem) {
  26.             $payload $lineItem->getPayload();
  27.             if ($payload['productNumber'] != null && str_contains($payload['productNumber'], 'NEO')) {
  28.                 $orderHasNeo true;
  29.             }
  30.         }
  31.         if ($orderHasNeo) {
  32.             $event->getPage()->assign(
  33.                 [
  34.                     'hideShippingAddress' => true,
  35.                 ]
  36.             );
  37.         }
  38.     }
  39.     public function onCheckoutConfirmLoaded($event): void
  40.     {
  41.         $this->hideDeliveryAdress($event);
  42.         $this->handleSepaLogic($event);
  43.     }
  44.     public function hideDeliveryAdress(CheckoutConfirmPageLoadedEvent $event): void
  45.     {
  46.         $cartHasNeo false;
  47.         $neoLineItemId '';
  48.         $lastAddedLineItemId '';
  49.         $cart $event->getPage()->getCart();
  50.         $flatItems $cart->getLineItems()->getFlat();
  51.         foreach ($flatItems as $lineItem) {
  52.             $payload $lineItem->getPayload();
  53.             if ($payload['productNumber'] != null && str_contains($payload['productNumber'], 'NEO')) {
  54.                 $cartHasNeo true;
  55.             }
  56.         }
  57.         if ($cartHasNeo) {
  58.             $event->getPage()->assign(
  59.                 [
  60.                     'hideShippingAddress' => true,
  61.                 ]
  62.             );
  63.         }
  64.     }
  65.     private function handleSepaLogic(CheckoutConfirmPageLoadedEvent $event): void {
  66.         $customer $event->getSalesChannelContext()->getCustomer();
  67.         if ($customer === null) {
  68.             return;
  69.         }
  70.         $activeBillingAddress $customer->getActiveBillingAddress();
  71.         if ($activeBillingAddress === null) {
  72.             return;
  73.         }
  74.         $addressCustomFields  $activeBillingAddress->getCustomFields() ?? [];
  75.         $customerCustomFields $customer->getCustomFields() ?? [];
  76.         $nwbAddressId $addressCustomFields['custom_address_attributs_nwb_address_id'] ?? null;
  77.         $nwbAccountId $customerCustomFields['custom_customer_attributes_account_id'] ?? null;
  78.         if ($nwbAddressId === null || $nwbAccountId === null) {
  79.             return;
  80.         }
  81.         $activeBisAddress $this->sepaService
  82.             ->getCurrentBillingAddressInformation($nwbAddressId$nwbAccountId);
  83.         if ($activeBisAddress === null) {
  84.             return;
  85.         }
  86.         $iban $activeBisAddress->getIban();
  87.         $accountHolder $activeBisAddress->getAccountHolder();
  88.         if ($iban === null || $accountHolder === null) {
  89.             return;
  90.         }
  91.         $anonymisedIban $this->sepaService->anonymiseIban($iban);
  92.         $event->getPage()->assign([
  93.             'imnxxSepaInfos' => [
  94.                 'iban' => $anonymisedIban,
  95.                 'accountHolder' => $accountHolder
  96.             ]
  97.         ]);
  98.     }
  99. }