Shopware:为客户存储自定义字段

PiT*_*ber 1 shopware

我将此自定义字段添加到我的客户和注册表中storefront/component/account/register.html.twig

<input type="checkbox" class="custom-control-input" id="alumni" name="custom_kw_dau" value="1">
Run Code Online (Sandbox Code Playgroud)

该字段是类型复选框。它在后端工作正常,但在客户注册期间未填写。

PiT*_*ber 5

您必须手动存储它。订阅事件并将字段添加到customFields输出中,如下所示:

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;
}
Run Code Online (Sandbox Code Playgroud)