custom/plugins/426-fourtwosixregistrationfields/src/Subscriber/CustomerEventsSubscriber.php line 63

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.             $billingAddress $data->get('billingAddress');
  26.             $billingAddress->add([
  27.                 'customFields' => $this->getCustomFields($billingAddress),
  28.             ]);
  29.         }
  30.         if ($data->has('shippingAddress')) {
  31.             $shippingAddress $data->get('shippingAddress');
  32.             $shippingAddress->add([
  33.                 'customFields' => $this->getCustomFields($shippingAddress),
  34.             ]);
  35.         }
  36.     }
  37.     public function onUpsertAddress(DataMappingEvent $event)
  38.     {
  39.         $data $event->getInput();
  40.         $address $event->getOutput();
  41.         $address['customFields'] = $this->getCustomFields($data);
  42.         $event->setOutput($address);
  43.     }
  44.     private function getCustomFields(DataBag $data): array
  45.     {
  46.         return [
  47.             FourtwosixRegistrationFields::FISCAL_CODE_ATTRIBUTE_NAME => strtoupper($data->get('fiscalCode') ?? ''),
  48.             FourtwosixRegistrationFields::PEC_ADDRESS_ATTRIBUTE_NAME => strtolower($data->get('pecAddress') ?? ''),
  49.             FourtwosixRegistrationFields::RECIPIENT_CODE_ATTRIBUTE_NAME => strtoupper($data->get('recipientCode') ?? ''),
  50.             FourtwosixRegistrationFields::COMPANY_TYPE_ATTRIBUTE_NAME => $data->get('companyType'),
  51.         ];
  52.     }
  53.     public function onAddressUpdateMapping(DataMappingEvent $event)
  54.     {
  55.         $data $event->getInput();
  56.         $address $event->getOutput();
  57.         if (!$data->has('customFields')) {
  58.             return;
  59.         }
  60.         $customFields $data->get('customFields');
  61.         $address['customFields'] = $customFields;
  62.         $event->setOutput($address);
  63.     }
  64. }