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

Shopware 6:如何在商品列表页显示自定义字段值?

解决Shopware 6商品列表页自定义字段不显示的问题

问题原因

列表页与商品详情页的数据上下文和查询逻辑不同:

  • 详情页的page.product是完整的商品对象,默认包含自定义字段数据;
  • 列表页的商品数据是批量查询返回的,Shopware默认不会自动加载自定义字段,且模板中变量引用错误(误用了详情页的page.product变量)。

解决步骤

1. 修正Twig模板变量引用

列表页的商品是循环遍历的product对象,而非详情页的page.product。修改你的box-standard.html.twig:

{% sw_extends '@Storefront/storefront/component/product/card/box-standard.html.twig' %}

{% block component_product_box_info %}
    {{ parent() }}

    <div class="test">
        <p>{{ product.translated.customFields.custom_events_dates }}</p>
    </div>    
{% endblock %}

2. 扩展列表查询,加载自定义字段

Shopware列表页默认不会查询自定义字段,需要通过事件订阅器强制添加字段关联:

创建事件订阅器文件

在你的插件目录下创建src/Subscriber/ProductListingCriteriaSubscriber.php:

<?php declare(strict_types=1);

namespace YourPluginNamespace\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ProductListingCriteriaSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => 'addCustomFieldToListing',
        ];
    }

    public function addCustomFieldToListing(ProductListingCriteriaEvent $event): void
    {
        $criteria = $event->getCriteria();
        // 添加自定义字段关联
        $criteria->addAssociation('customFields');
        // 确保加载翻译后的自定义字段
        $criteria->addAssociation('translated.customFields');
    }
}

注册订阅器

在插件的src/Resources/config/services.xml中添加订阅器注册:

<service id="YourPluginNamespace\Subscriber\ProductListingCriteriaSubscriber">
    <tag name="kernel.event_subscriber"/>
</service>

3. 清除缓存

执行Shopware缓存清理命令,确保修改生效:

bin/console cache:clear

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 13:10:42