Shopware 6应用:如何为各店面创建独立可翻译的配置文本字段?
解决多店面独立可翻译Snippet/自定义字段问题
方法一:使用销售渠道专属Snippet
直接修改你的sw-snippet-field组件,添加销售渠道关联属性,让字段读取和保存当前销售渠道的专属snippet值:
<component name="sw-snippet-field"> <name>leftBoxTitle</name> <label>Title</label> <label lang="de-DE">Titel</label> <snippet>minds-atcch.leftBoxTitle</snippet> <!-- 绑定当前路由中的销售渠道ID,确保在销售渠道配置上下文生效 --> <sales-channel-id>{{ $route.params.salesChannelId }}</sales-channel-id> </component>
原理说明
添加sales-channel-id属性后,该字段会将snippet值与指定的销售渠道绑定,不再使用全局snippet。后续在不同销售渠道的配置页修改该字段时,只会更新对应销售渠道的snippet数据,其他店面不会同步变更,同时仍保留多语言翻译能力。
方法二:创建销售渠道绑定的自定义字段
如果更倾向于用独立的自定义字段而非snippet,可按以下步骤配置:
1. 定义自定义字段
在插件的src/Resources/config/custom_field.json中添加销售渠道关联的自定义字段:
{ "name": "mindsAtcchLeftBoxTitle", "label": "Left Box Title", "labelDe": "Linke Box Titel", "type": "text", "customFieldSet": { "name": "mindsAtcchSalesChannelSettings", "label": "Minds ATCC Sales Channel Settings", "labelDe": "Minds ATCC Vertriebskanal-Einstellungen", "relations": [ { "entityName": "sales_channel" } ] } }
2. 替换为自定义输入字段
将原sw-snippet-field替换为sw-text-field,绑定到销售渠道的自定义字段:
<component name="sw-text-field"> <name>mindsAtcchLeftBoxTitle</name> <label>Title</label> <label lang="de-DE">Titel</label> <value>{% if $salesChannel.customFields.mindsAtcchLeftBoxTitle %}{{ $salesChannel.customFields.mindsAtcchLeftBoxTitle }}{% endif %}</value> </component>
多语言适配(可选)
若需要支持多语言,可创建多语言类型的自定义字段(Shopware 6.4及以上版本支持),修改type为text_translatable即可:
"type": "text_translatable"
验证方法
修改配置后,进入不同销售渠道的对应配置页面,分别修改字段值,切换到其他销售渠道检查,确认各自的内容独立,不会同步变更。
内容的提问来源于stack exchange,提问作者Leon Schwanitz
相关产品推荐
相关产品推荐

