custom/plugins/KlarnaPayment/src/Components/EventListener/DataMappingEventListener.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Installer\Modules\CustomFieldInstaller;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\Event\DataMappingEvent;
  7. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. // TODO[KLARNASUPPORT-833]: remove this downwards compatibility for order_address custom field saving after 6.4.14.0
  10. // tip: search for changes around the commit for this line or "KLARNASUPPORT-833" in the git history
  11. class DataMappingEventListener implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING  => 'addCustomFieldsToAddress',
  17.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'addCustomFieldsToAddress',
  18.             CustomerEvents::MAPPING_ADDRESS_CREATE            => 'addCustomFieldsToAddress',
  19.         ];
  20.     }
  21.     public function addCustomFieldsToAddress(DataMappingEvent $event): void
  22.     {
  23.         $input  $event->getInput();
  24.         $output $event->getOutput();
  25.         // Class will be available by the commit from 6.4.14.0 with the saving of custom fields
  26.         // See https://github.com/shopware/platform/commit/c6d66b3c961b113285f895338aeeecd55b26a0ad
  27.         if (class_exists('\Shopware\Core\System\SalesChannel\StoreApiCustomFieldMapper')) {
  28.             return;
  29.         }
  30.         $requestCustomFields $input->get('customFields');
  31.         if (!$requestCustomFields instanceof RequestDataBag) {
  32.             return;
  33.         }
  34.         $output['customFields'] = array_merge(
  35.             $output['customFields'] ?? [],
  36.             [
  37.                 CustomFieldInstaller::FIELD_KLARNA_CUSTOMER_ENTITY_TYPE     => $requestCustomFields->get(CustomFieldInstaller::FIELD_KLARNA_CUSTOMER_ENTITY_TYPE),
  38.                 CustomFieldInstaller::FIELD_KLARNA_CUSTOMER_REGISTRATION_ID => $requestCustomFields->get(CustomFieldInstaller::FIELD_KLARNA_CUSTOMER_REGISTRATION_ID),
  39.             ]
  40.         );
  41.         $event->setOutput($output);
  42.     }
  43. }