<?php
declare(strict_types=1);
namespace Proc\ProcWegReg\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
class CustomerRegistrationEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomerUpdateMapping',
CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onCustomerUpdateMappingAddress'
];
}
public function onCustomerUpdateMapping(DataMappingEvent $event)
{
$data = $event->getInput();
$customer = $event->getOutput();
$allowedData = [
"personalFirmenWebseite" => "personal_firmen_webseite",
"registrationComment" => "registration_comment",
"personalKooperation" => "personal_kooperation",
"sapCustomerNumber" => "sap_customer_number",
"alreadyCustomerCheckbox" => "already_customer_checkbox"
];
foreach ($allowedData as $key => $customFieldName ) {
if ($data->has($key)) {
$value = $data->get($key);
if($key == 'alreadyCustomerCheckbox') {
$value = (bool) $value;
}
$customer['customFields']['additional_custom_customer_'. $customFieldName] = $value;
}
}
return $event->setOutput($customer);
}
public function onCustomerUpdateMappingAddress (DataMappingEvent $event) {
$data = $event->getInput();
$address = $event->getOutput();
$allowedData = [
"handyNummer" => "handy_nummer"
];
foreach ($allowedData as $key => $customFieldName ) {
if ($data->has($key)) {
$value = $data->get($key);
$address['customFields']['custom_address_data_'. $customFieldName] = $value;
}
}
return $event->setOutput($address);
}
}