Shopware:为客户存储自定义字段
Shopware: Store custom field for customer
我将此自定义字段添加到我的客户和 storefront/component/account/register.html.twig
中的注册表中:
<input type="checkbox" class="custom-control-input" id="alumni" name="custom_kw_dau" value="1">
该字段是类型复选框。后台运行正常,但客户注册时未填写。
您必须手动存储它。订阅事件并将字段添加到输出中的 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;
}
是的,您需要订阅该活动 - 但我是这样做的,而不是上面的活动,而且效果也很好。
public static function getSubscribedEvents(): array
{
return [
CustomerRegisterEvent::class => 'onRegister',
GuestCustomerRegisterEvent::class => 'onRegister'
];
}
public function onRegister(CustomerRegisterEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$params = $request->request->all();
$customer = $event->getCustomer();
$data = [
'id' => $customer->getId(),
'customFields' => [
'your_field' => $params['your_field']
]
];
$this->customerRepository->update([$data], $event->getContext());
}
}
但我认为上面的答案可能更合适,因为它不需要任何额外的服务。
我将此自定义字段添加到我的客户和 storefront/component/account/register.html.twig
中的注册表中:
<input type="checkbox" class="custom-control-input" id="alumni" name="custom_kw_dau" value="1">
该字段是类型复选框。后台运行正常,但客户注册时未填写。
您必须手动存储它。订阅事件并将字段添加到输出中的 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;
}
是的,您需要订阅该活动 - 但我是这样做的,而不是上面的活动,而且效果也很好。
public static function getSubscribedEvents(): array
{
return [
CustomerRegisterEvent::class => 'onRegister',
GuestCustomerRegisterEvent::class => 'onRegister'
];
}
public function onRegister(CustomerRegisterEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$params = $request->request->all();
$customer = $event->getCustomer();
$data = [
'id' => $customer->getId(),
'customFields' => [
'your_field' => $params['your_field']
]
];
$this->customerRepository->update([$data], $event->getContext());
}
}
但我认为上面的答案可能更合适,因为它不需要任何额外的服务。