custom/plugins/SteincoProducts/src/Subscriber/CustomerAddCustomFieldsSubscriber.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Schoettler\SteincoProducts\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Framework\Event\DataMappingEvent;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. class CustomerAddCustomFieldsSubscriber implements EventSubscriberInterface
  7. {
  8.     public static function getSubscribedEvents(): array
  9.     {
  10.         return [
  11.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'addCustomerCustomFields',
  12.             CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'saveCustomerCustomFields'
  13.         ];
  14.     }
  15.     public function addCustomerCustomFields(DataMappingEvent $event)
  16.     {
  17.         $inputData $event->getInput();
  18.         $outputData $event->getOutput();
  19.         $website_dsgvo $inputData->get('acceptWebsiteDsgvo'false);
  20.         // why check again? 
  21.         // In case $website_dsgvo is null we need a bool value. This will make sure it really is a bool.
  22.         $website_dsgvo = ($website_dsgvo)? true false;
  23.         $ads_consent $inputData->get('adsConsent'false);
  24.         $ads_consent = ($ads_consent) ? true false;
  25.         if($ads_consent) {
  26.             $outputData['newsletter'] = true;
  27.         }
  28.         $outputData['customFields'] = array(
  29.             'websiteDsgvo' => $website_dsgvo,
  30.             'adsConsent' => $ads_consent);
  31.         $customer_no $inputData->get('steincoCustno'false);
  32.         if($customer_no){
  33.             $outputData['customFields']['custom_customer_steinco_custno'] = $customer_no;
  34.         }
  35.         $outputData['company'] = $inputData->get('company'false);
  36.         // $outputData['vatIds'][] = $inputData->get('vatIds', false)[0];
  37.         $outputData['vatIds'][] = (is_array($inputData->get('vatIds'false)) && isset($inputData->get('vatIds'false)[0])) ? $inputData->get('vatIds'false)[0] : [];
  38.         $event->setOutput($outputData);
  39.         return true;
  40.     }
  41.     public function saveCustomerCustomFields(DataMappingEvent $event)
  42.     {
  43.         $inputData $event->getInput();
  44.         $outputData $event->getOutput();
  45.         $outputData['customFields'] = array(
  46.             'custom_customer_steinco_custno' => '');
  47.         $customer_no $inputData->get('steincoCustno'false);
  48.         if($customer_no){
  49.             $outputData['customFields']['custom_customer_steinco_custno'] = $customer_no;
  50.         }
  51.         $outputData['company'] = $inputData->get('company'false);
  52.         $outputData['vatIds'][] = (is_array($inputData->get('vatIds'false)) && isset($inputData->get('vatIds'false)[0])) ? $inputData->get('vatIds'false)[0] : [];
  53.         // $outputData['vatIds'][0] = $inputData->get('vatIds', false)[0];
  54.         $event->setOutput($outputData);
  55.         return true;
  56.     }
  57. }