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

Shopware 6 Twig模板中如何判断客户是否属于特定客户组?

Shopware 6 Twig模板检查客户是否属于特定客户组

首先要确保客户已登录(即context.customer存在),再结合客户组信息进行判断,以下是几种常见场景的实现方式:

检查客户是否属于单个特定客户组

假设目标客户组的技术名称为wholesale(后台客户组设置中的技术标识,不是显示名称),可以用以下简洁写法:

{% if context.customer is defined and context.customer.groups|some(g => g.technicalName == 'wholesale') %}
    <!-- 仅对该客户组显示的内容 -->
{% endif %}

some过滤器会检查客户组集合中是否存在满足条件的项,只要有一个匹配就返回true。

也可以用filter过滤后判断结果是否为空:

{% if context.customer is defined and context.customer.groups|filter(g => g.technicalName == 'wholesale') is not empty %}
    <!-- 仅对该客户组显示的内容 -->
{% endif %}

检查客户是否属于多个特定客户组中的任意一个

如果需要判断客户是否在wholesale、vip等多个组中的任意一个,可先定义目标组数组,再进行匹配:

{% set targetGroupTechNames = ['wholesale', 'vip'] %}
{% if context.customer is defined and context.customer.groups|some(g => targetGroupTechNames contains g.technicalName) %}
    <!-- 对任意匹配的客户组显示的内容 -->
{% endif %}

注意:必须使用客户组的technicalName(后台设置的技术标识)进行匹配,不要用显示名称,避免因多语言或名称修改导致判断失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 00:44:52