custom/plugins/ImnxxCustomerHelper/src/Subscriber/AddressSubscriber.php line 151

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ImnxxCustomerHelper\Subscriber;
  3. use ImnxxCustomerHelper\Service\HelperService;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  6. use stdClass;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class AddressSubscriber implements EventSubscriberInterface
  9. {
  10.     private HelperService $helperService;
  11.     public function __construct(HelperService $helperService)
  12.     {
  13.         $this->helperService $helperService;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             GenericPageLoadedEvent::class => 'onPageLoaded',
  19.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent',
  20.         ];
  21.     }
  22.     public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event)
  23.     {
  24.         $_SESSION['GET_ADDRESSES'] = true;
  25.     }
  26.     private function getNwbGuestAddresses($addressGroupId$nwbService)
  27.     {
  28.         $curl curl_init('https://'.$nwbService.'/customercare/api/addresses/GetVisitorAddresses/' $addressGroupId);
  29.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  30.         $nwbAccountAddresses json_decode(curl_exec($curl));
  31.         curl_close($curl);
  32.         return $nwbAccountAddresses;
  33.     }
  34.     private function getNwbAddresses($nwbAccountId$nwbSessionId$nwbService)
  35.     {
  36.         $curl curl_init('https://'.$nwbService.'/customercare/api/addresses/GetUserAddresses/' $nwbAccountId);
  37.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  38.         curl_setopt($curlCURLOPT_HTTPHEADER, ['x-nwb-sessionid: ' $nwbSessionId]);
  39.         curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  40.         $nwbAccountAddresses json_decode(curl_exec($curl));
  41.         curl_close($curl);
  42.         return $nwbAccountAddresses;
  43.     }
  44.     private function getAddressState($swAddresses$nwbAddresses): stdClass
  45.     {
  46.         $addressStatus = new stdClass();
  47.         $addressStatus->delete = [];
  48.         $addressStatus->update = [];
  49.         $addressStatus->add = [];
  50.         $swAddressIds = [];
  51.         $nwbAddressIds = [];
  52.         foreach ($swAddresses as $swAddress) {
  53.             if($swAddress->getCustomFields()['custom_address_attributs_nwb_address_id'] !== 0) {
  54.                 $swAddressIds[$swAddress->getCustomFields()['custom_address_attributs_nwb_address_id']] = $swAddress;
  55.             }
  56.         }
  57.         foreach ($nwbAddresses->Addresses as $nwbAddress) {
  58.             $nwbAddressIds[$nwbAddress->ID] = $nwbAddress;
  59.         }
  60.         foreach ($nwbAddressIds as $nwbId => $nwbAddress) {
  61.             if (isset($swAddressIds[$nwbId])) {
  62.                 $addressStatus->update[$nwbId] = $swAddressIds[$nwbId];
  63.             } else {
  64.                 $addressStatus->add[$nwbId] = $nwbAddress;
  65.             }
  66.         }
  67.         foreach ($swAddressIds as $nwbId => $swAddress) {
  68.             if (!isset($addressStatus->update[$nwbId])) {
  69.                 $addressStatus->delete[$swAddress->getId()] = $swAddress;
  70.             }
  71.         }
  72.         return $addressStatus;
  73.     }
  74.     public function convertNwbAddressToSwAddress($nwbAddress$customerId$context): array
  75.     {
  76.         if ($nwbAddress->Country != 'DE') {
  77.             // get Country ID, when not germany
  78.             $nwbAddress->Country_id $this->helperService->getCountryIdByIso($nwbAddress->Country$context);
  79.         } else {
  80.             $nwbAddress->Country_id $this->helperService->getCountryIdByIso('DE'$context);
  81.         }
  82.         if (strtolower($nwbAddress->Salutation) == 'herr') {
  83.             $nwbAddress->Salutation_id '5d6a01a988104035adc1e822e2d2ca7e';
  84.         } elseif (strtolower($nwbAddress->Salutation) == 'frau') {
  85.             $nwbAddress->Salutation_id '7ae9a14f06db435bbbd95bcb0a92cf17';
  86.         } else {
  87.             $nwbAddress->Salutation_id '40a8fca759e04bb5a372711c7aa63e10';
  88.         }
  89.         $phoneNr '';
  90.         if (
  91.             isset($nwbAddress->TelephoneCityPrefix) &&
  92.             $nwbAddress->TelephoneCityPrefix != '' &&
  93.             isset($nwbAddress->TelephoneNumber) &&
  94.             $nwbAddress->TelephoneNumber != ''
  95.         ) {
  96.             $phoneNr $nwbAddress->TelephoneCityPrefix '/' $nwbAddress->TelephoneNumber;
  97.         }
  98.         $street $nwbAddress->Street;
  99.         $streetNumber $nwbAddress->Streetnumber;
  100.         $salutationId $nwbAddress->Salutation_id;
  101.         $address = [
  102.             'customerId' => $customerId,
  103.             'company' => isset($nwbAddress->Institution) ? $nwbAddress->Institution '',
  104.             'department' => isset($nwbAddress->Department) ? $nwbAddress->Department '',
  105.             'countryId' => $nwbAddress->Country_id,
  106.             'salutationId' => $salutationId,
  107.             'firstName' => $nwbAddress->Firstname,
  108.             'lastName' => $nwbAddress->Name,
  109.             'zipcode' => $nwbAddress->Postalcode,
  110.             'city' => $nwbAddress->City,
  111.             'street' => $street ' ' $streetNumber,
  112.             'phoneNumber' => $phoneNr,
  113.             'customFields' => [
  114.                 'custom_address_attributs_nwb_address_id' => $nwbAddress->ID,
  115.                 'acris_separate_street_address_street' => $street,
  116.                 'acris_separate_street_address_house_number' => $streetNumber,
  117.                 'iban' => isset($nwbAddress->IBAN) ? $nwbAddress->IBAN '',
  118.                 'account_holder' => isset($nwbAddress->AccountHolder) ? $nwbAddress->AccountHolder '',
  119.             ],
  120.         ];
  121.         if ($nwbAddress->Type == 0) {
  122.             $address['customFields']['nwb_address_is_billing'] = true;
  123.         }
  124.         return $address;
  125.     }
  126.     public function onPageLoaded(GenericPageLoadedEvent $event)
  127.     {
  128.         $nwbService $event->getSalesChannelContext()->getSalesChannel()->getCustomFields()['bis_url'];
  129.         if (
  130.             $event->getSalesChannelContext()->getCustomer() != null &&
  131.             (
  132.                 (
  133.                     isset($_SERVER['HTTP_REFERER']) &&
  134.                     str_contains($_SERVER['HTTP_REFERER'], $nwbService)
  135.                 ) ||
  136.                 (
  137.                     isset($_SESSION['GET_ADDRESSES']) && !str_contains($_SERVER['REQUEST_URI'], '/checkout/finish')
  138.                 )
  139.             )
  140.         ) {
  141.             $customer $this->helperService->getCustomerWithId(
  142.                 $event->getContext(),
  143.                 $event->getSalesChannelContext()->getCustomer()->getId()
  144.             );
  145.             $nwbAccountId = isset($customer->getCustomFields()['custom_customer_attributes_account_id'])
  146.                 ? $customer->getCustomFields()['custom_customer_attributes_account_id']
  147.                 : null;
  148.             $nwbSessionId = isset($customer->getCustomFields()['custom_customer_attributes_session_id'])
  149.                 ? $customer->getCustomFields()['custom_customer_attributes_session_id']
  150.                 : null;
  151.             $addressGroupId $_SESSION['addressGroupId'] ?? null;
  152.             $nwbAddresses null;
  153.             if ($addressGroupId != null) {
  154.                 $nwbAddresses $this->getNwbGuestAddresses($_SESSION['addressGroupId'], $nwbService);
  155.             }
  156.             if ($nwbAccountId) {
  157.                 $nwbAddresses $this->getNwbAddresses($nwbAccountId$nwbSessionId$nwbService);
  158.             }
  159.             if ($nwbAddresses != null) {
  160.                 $swAddresses $this->helperService->getCustomerAddresses($customer->getId(), $event->getContext());
  161.                 $addressStatus $this->getAddressState($swAddresses$nwbAddresses);
  162.                 foreach ($addressStatus->update as $nwbAddressId => $swAddress) {
  163.                     $toUpdateNwbAddress null;
  164.                     foreach ($nwbAddresses->Addresses as $nwbAddress) {
  165.                         if ($nwbAddressId == $nwbAddress->ID) {
  166.                             $toUpdateNwbAddress $nwbAddress;
  167.                         }
  168.                     }
  169.                     $convertedAddress $this->convertNwbAddressToSwAddress($toUpdateNwbAddress$customer->getId(), $event->getContext());
  170.                     $this->helperService->updateCustomerAddress($swAddress->getId(), $convertedAddress$event->getContext());
  171.                 }
  172.                 foreach ($addressStatus->add as $nwbAddressId => $nwbAddress) {
  173.                     $convertedAddress $this->convertNwbAddressToSwAddress($nwbAddress$customer->getId(), $event->getContext());
  174.                     $this->helperService->addCustomerAddress($convertedAddress$event->getContext());
  175.                 }
  176.                 if ($nwbSessionId) {
  177.                     $swAddresses $this->helperService->getCustomerAddresses($customer->getId(), $event->getContext());
  178.                     $nwbAddressIdForDefaultBillingAddress null;
  179.                     $nwbAddressIdForDefaultShippingAddress null;
  180.                     $nwbShippingAddresses = [];
  181.                     foreach ($nwbAddresses->Addresses as $nwbAddress) {
  182.                         if ($nwbAddress->Type == 1) {
  183.                             if ( $nwbAddress->IsDefaultAddress && is_null($nwbAddressIdForDefaultShippingAddress) ) {
  184.                                 $nwbAddressIdForDefaultShippingAddress $nwbAddress->ID;
  185.                             }
  186.                             if ( isset($_GET['addressId']) && $_GET['addressId'] == $nwbAddress->ID ) {
  187.                                 $nwbAddressIdForDefaultShippingAddress $nwbAddress->ID;
  188.                             }
  189.                             $nwbShippingAddresses[] = $nwbAddress;
  190.                         }
  191.                         if ($nwbAddress->Type == /*&& $nwbAddress->IsDefaultAddress*/) {
  192.                             $nwbAddressIdForDefaultBillingAddress $nwbAddress->ID;
  193.                         }
  194.                     }
  195.                     if ($nwbAddressIdForDefaultShippingAddress == null) {
  196.                         $nwbAddressIdForDefaultShippingAddress array_shift($nwbShippingAddresses);
  197.                         if($nwbAddressIdForDefaultShippingAddress == null){
  198.                             $nwbAddressIdForDefaultShippingAddress $nwbAddressIdForDefaultBillingAddress;
  199.                         }
  200.                     }
  201.                     foreach ($swAddresses as $swAddress) {
  202.                         if ($swAddress->getCustomFields()['custom_address_attributs_nwb_address_id'] == $nwbAddressIdForDefaultBillingAddress) {
  203.                             $paymentId $this->helperService->getPaymentMethodIdByName('Kauf auf Rechnung'$event->getContext());
  204.                             $this->helperService->updateCustomer(
  205.                                 $customer->getId(),
  206.                                 [
  207.                                     'defaultBillingAddressId' => $swAddress->getId(),
  208.                                     'defaultPaymentMethodId' => $paymentId
  209.                                 ],
  210.                                 $event->getContext()
  211.                             );
  212.                         }
  213.                         if (
  214.                             $nwbAddressIdForDefaultShippingAddress != null &&
  215.                             $swAddress->getCustomFields()['custom_address_attributs_nwb_address_id'] == $nwbAddressIdForDefaultShippingAddress
  216.                         ) {
  217.                             $this->helperService->updateCustomer(
  218.                                 $customer->getId(),
  219.                                 ['defaultShippingAddressId' => $swAddress->getId()],
  220.                                 $event->getContext()
  221.                             );
  222.                         }
  223.                     }
  224.                 }
  225.                 foreach ($addressStatus->delete as $swId => $swAddress) {
  226.                     $this->helperService->deleteCustomerAddress($swId$event->getContext());
  227.                 }
  228.             }
  229.             if(isset($_SESSION['GET_ADDRESSES'])){
  230.                 unset($_SESSION['GET_ADDRESSES']);
  231.             }
  232.             header('Refresh: 0');
  233.             die();
  234.         }
  235.     }
  236. }