custom/plugins/FourtwosixRegistrationFields/src/Subscriber/CustomerEventsSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixRegistrationFields\Subscriber;
  4. use FourtwosixRegistrationFields\FourtwosixRegistrationFields;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Checkout\Customer\CustomerEvents;
  7. use Shopware\Core\Framework\Event\DataMappingEvent;
  8. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  9. class CustomerEventsSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onUpsertAddress',
  15.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomerUpdateMapping',
  16.             CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'onCustomerUpdateMapping',
  17.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onAddressUpdateMapping',
  18.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onAddressUpdateMapping',
  19.         ];
  20.     }
  21.     public function onCustomerUpdateMapping(DataMappingEvent $event)
  22.     {
  23.         $data $event->getInput();
  24.         if (!$data->has('billingAddress')) {
  25.             return;
  26.         }
  27.         $billingAddress $data->get('billingAddress');
  28.         $billingAddress->add([
  29.             'customFields' => $this->getCustomFields($billingAddress),
  30.         ]);
  31.         if (!$data->has('shippingAddress')) {
  32.             return;
  33.         }
  34.         $shippingAddress $data->get('shippingAddress');
  35.         $shippingAddress->add([
  36.             'customFields' => $this->getCustomFields($shippingAddress),
  37.         ]);
  38.     }
  39.     public function onUpsertAddress(DataMappingEvent $event)
  40.     {
  41.         $data $event->getInput();
  42.         $address $event->getOutput();
  43.         $address['customFields'] = $this->getCustomFields($data);
  44.         $event->setOutput($address);
  45.     }
  46.     private function getCustomFields(DataBag $data): array
  47.     {
  48.         return [
  49.             FourtwosixRegistrationFields::FISCAL_CODE_ATTRIBUTE_NAME => ($data->get('fiscalCode')) ? strtoupper($data->get('fiscalCode')) : "",
  50.             FourtwosixRegistrationFields::PEC_ADDRESS_ATTRIBUTE_NAME => $data->get('pecAddress'),
  51.             FourtwosixRegistrationFields::RECIPIENT_CODE_ATTRIBUTE_NAME => $data->get('recipientCode'),
  52.             FourtwosixRegistrationFields::COMPANY_TYPE_ATTRIBUTE_NAME => $data->get('companyType'),
  53.             FourtwosixRegistrationFields::CONTACT_ATTRIBUTE_NAME => $data->get('contact') ?? '',
  54.         ];
  55.     }
  56.     public function onAddressUpdateMapping(DataMappingEvent $event)
  57.     {
  58.         $data $event->getInput();
  59.         $address $event->getOutput();
  60.         if (!$data->has('customFields')) {
  61.             return;
  62.         }
  63.         $customFields $data->get('customFields');
  64.         $address['customFields'] = $customFields;
  65.         $event->setOutput($address);
  66.     }
  67. }