custom/plugins/EsmComputer/src/Storefront/Subscriber/AddressSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EsmComputer\Storefront\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Framework\Event\DataMappingEvent;
  5. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  6. use Shopware\Core\Framework\Validation\DataValidator;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Validator\Constraints\NotBlank;
  9. use Symfony\Component\Validator\Constraints\Type;
  10. class AddressSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var DataValidator
  14.      */
  15.     private $dataValidator;
  16.     public function __construct(
  17.         DataValidator $dataValidator
  18.     ) {
  19.         $this->dataValidator $dataValidator;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onDataMappingEvent',
  25.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onDataMappingEvent',
  26.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onDataMappingEvent'
  27.         ];
  28.     }
  29.     /**
  30.      * @param DataMappingEvent $event
  31.      */
  32.     public function onDataMappingEvent(DataMappingEvent $event): void
  33.     {
  34.         $this->setAddressOutput($event);
  35.     }
  36.     /**
  37.      * @param DataMappingEvent $event
  38.      */
  39.     private function setAddressOutput(DataMappingEvent $event): void
  40.     {
  41.         // Ignore this subscriber on PayPal express checkout
  42.         if($event->getContext()->getExtension('payPalExpressCheckoutActive')) {
  43.             return;
  44.         }
  45.         $output $event->getOutput();
  46.         $houseNumber $event->getInput()->get('houseNumber');
  47.         $definition = new DataValidationDefinition();
  48.         $definition->add('houseNumber', new NotBlank(), new Type('string'));
  49.         $this->dataValidator->validate($event->getInput()->all(), $definition);
  50.         if (!$houseNumber) {
  51.             return;
  52.         }
  53.         $output['street'] = trim($output['street'])." ".trim($houseNumber);
  54.         if (!array_key_exists('customFields'$output)) {
  55.             $output['customFields'] = [
  56.                 'custom_address_separate_street' => trim($event->getInput()->get('street')),
  57.                 'custom_address_separate_house_number' => trim($event->getInput()->get('houseNumber'))
  58.             ];
  59.         } else {
  60.             $output['customFields']['custom_address_separate_street'] = trim($event->getInput()->get('street'));
  61.             $output['customFields']['custom_address_separate_house_number'] = trim($event->getInput()->get('houseNumber'));
  62.         }
  63.         $event->setOutput($output);
  64.     }
  65. }