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

Shopware 6:仅在订单有评论时显示评论图标实现疑问

解决Shopware 6订单列表仅显示含客户评论的图标问题

问题核心是订单列表的API默认未返回customerComment字段,所以你在模板里访问item.customerComment始终为null。需要先扩展订单列表的数据查询逻辑,确保该字段被加载,再调整显示判断。

步骤1:扩展订单列表的数据查询

修改你的app/administration/src/module/sw-order/page/sw-order-list/index.js,添加对列表查询条件的扩展,把customerComment加入返回字段:

import template from './sw-order-list.html.twig';

Shopware.Component.override('sw-order-list', {
    template,
    
    methods: {
        getListCriteria() {
            // 调用父类的查询条件
            const criteria = this.$super('getListCriteria');
            // 关联orderCustomer表并选择customerComment字段
            criteria.addAssociation('orderCustomer');
            criteria.addSelect('orderCustomer.customerComment');
            return criteria;
        }
    }
});

注意:customerComment实际存储在order_customer关联表中,必须先关联该表才能获取对应字段。

步骤2:调整模板显示逻辑

修改你的twig模板,修正字段访问路径,并添加更严谨的非空判断:

{% block sw_order_list_grid_columns_order_number %}
    {% parent %}

    <template #column-orderNumber="{ item }">
        <sw-button
            v-if="item.orderCustomer?.customerComment && item.orderCustomer.customerComment.trim().length > 0"
            v-tooltip="{ message: item.orderCustomer.customerComment }"
            class="sw-order-list__tooltip-order-comment"
            size="x-small"
        >
            <sw-icon
                name="regular-comments"
                size="14px"
            />
        </sw-button>
    </template>
{% endblock %}

关键说明

  • 字段获取逻辑:Shopware 6的Admin API默认只返回核心字段,关联表和非默认字段需要手动通过criteria添加查询规则
  • 空值判断:使用可选链(?.)避免空指针异常,同时判断去除空格后的内容长度,确保只有实际有内容的评论才显示图标

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 01:22:14