You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6 客户实体custom field翻译无法保存问题咨询

Shopware 6 原生支持customer实体自定义字段的翻译功能,你遇到的问题是配置和代码细节错误导致的,按以下步骤排查修复即可:


1. 检查自定义字段配置

你创建customer关联的自定义字段时,必须开启**允许翻译(Allow translation)**开关,该开关默认关闭,未开启的情况下系统不会处理你传入的翻译内容,只会存储默认语言的单值。
另外确认你代码中用到的$languageId是系统后台已配置的有效语言ID,避免存错语言维度。

2. 修正代码错误

你现有代码存在两处核心错误:

  • Repository的upsert方法第一个参数要求传入批量操作的二维数组,哪怕仅操作单条客户数据,也需要将客户数据数组外层包一层[],否则系统会解析错误,无法正确识别翻译字段。
  • 若你是更新已有客户数据,必须传入客户的id作为upsert的匹配依据,仅传customerNumber不会被系统默认识别为匹配键。

3. 可正常运行的参考代码

$customFieldsArr = [
    // 更新场景必填,新建场景可不填,系统自动生成
    'id' => $customerId,
    'translations' => [
        $languageId => [
            'customFields' => [
                $customFieldTechnicalName => $value
            ]
        ]
    ]
];

$customerData = [
    'customerNumber' => $customerNumber,
    'active' => $active,
    'title' => $title,
    'salutationId' => $salutationId,
    'firstName' => $firstname,
    'lastName' => $lastname,
    'company' => $company,
    'email' => $email,
    'birthday' => $birthday,
    'salesChannelId' => $salesChannelId,
    'vatIds' => $vatId,
    'groupId' => $customerGroupId
];

$customerData = array_merge($customFieldsArr, $customerData);

// 注意这里外层多了一层[],传入的是二维数组
$this->customerRepository->upsert([$customerData], $context);

4. 验证与调用

配置和代码修正后,可直接查看customer_translation数据表对应记录的custom_fields字段,确认多语言值是否已经正确存入。
前台结账页调用时,直接使用{{ context.customer.translated.customFields.xxx }}(xxx替换为你的自定义字段技术名)即可自动读取当前语言对应的翻译值。


内容的提问来源于stack exchange,提问作者Janine Kroser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 19:06:04