<?php declare(strict_types=1);
namespace Schoettler\SteincoProducts\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Checkout\Customer\CustomerEvents;
class CustomerAddCustomFieldsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'addCustomerCustomFields',
CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'saveCustomerCustomFields'
];
}
public function addCustomerCustomFields(DataMappingEvent $event)
{
$inputData = $event->getInput();
$outputData = $event->getOutput();
$website_dsgvo = $inputData->get('acceptWebsiteDsgvo', false);
// why check again?
// In case $website_dsgvo is null we need a bool value. This will make sure it really is a bool.
$website_dsgvo = ($website_dsgvo)? true : false;
$ads_consent = $inputData->get('adsConsent', false);
$ads_consent = ($ads_consent) ? true : false;
if($ads_consent) {
$outputData['newsletter'] = true;
}
$outputData['customFields'] = array(
'websiteDsgvo' => $website_dsgvo,
'adsConsent' => $ads_consent);
$customer_no = $inputData->get('steincoCustno', false);
if($customer_no){
$outputData['customFields']['custom_customer_steinco_custno'] = $customer_no;
}
$outputData['company'] = $inputData->get('company', false);
// $outputData['vatIds'][] = $inputData->get('vatIds', false)[0];
$outputData['vatIds'][] = (is_array($inputData->get('vatIds', false)) && isset($inputData->get('vatIds', false)[0])) ? $inputData->get('vatIds', false)[0] : [];
$event->setOutput($outputData);
return true;
}
public function saveCustomerCustomFields(DataMappingEvent $event)
{
$inputData = $event->getInput();
$outputData = $event->getOutput();
$outputData['customFields'] = array(
'custom_customer_steinco_custno' => '');
$customer_no = $inputData->get('steincoCustno', false);
if($customer_no){
$outputData['customFields']['custom_customer_steinco_custno'] = $customer_no;
}
$outputData['company'] = $inputData->get('company', false);
$outputData['vatIds'][] = (is_array($inputData->get('vatIds', false)) && isset($inputData->get('vatIds', false)[0])) ? $inputData->get('vatIds', false)[0] : [];
// $outputData['vatIds'][0] = $inputData->get('vatIds', false)[0];
$event->setOutput($outputData);
return true;
}
}