Search code examples
shopware

Shopware: Store custom field for customer


I added this custom field to my customer and to the register form in storefront/component/account/register.html.twig:

<input type="checkbox" class="custom-control-input" id="alumni" name="custom_kw_dau" value="1">

The field is type checkbox. It works fine in the backend but it is not filled during customer registration.


Solution

  • You have to manually store it. Subscribe to event and add the field to customFields in the output like this:

    public static function getSubscribedEvents(): array
    {
        return [
            CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'addCustomField'
        ];
    }
    
    public function addCustomField(DataMappingEvent $event): bool
    {
        $inputData = $event->getInput();
        $outputData = $event->getOutput();
    
        $custom_field = (bool)$inputData->get('custom_kw_dau', false);
        $outputData['customFields'] = array('custom_kw_dau' => $custom_field);
    
        $event->setOutput($outputData);
    
        return true;
    }