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

如何在Shopware 6后台搜索中添加产品自定义字段?

在Shopware 6后台搜索中添加产品自定义字段

要让产品的自定义字段能被后台搜索到,你得通过自定义插件扩展产品实体的搜索配置,具体操作步骤如下:

1. 准备自定义插件

如果还没有自己的自定义插件,先搭建一个基础插件结构——这是Shopware 6实现自定义功能的前提。

2. 配置搜索字段(XML方式)

在插件的src/Resources/config目录下新建search.xml文件,把要加入搜索的自定义字段配置进去:

<?xml version="1.0" encoding="UTF-8"?>
<search xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/System/Search/Resources/search.xsd">
    <entity name="product">
        <fields>
            <!-- 替换成你的自定义字段技术名称,比如custom_field_product_serial -->
            <field>custom_field_your_field</field>
        </fields>
    </entity>
</search>

接着在插件的src/Resources/config/services.xml里注册这个配置,让Shopware能加载它:

<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="YourPluginName\Search\ProductSearchExtension" public="true">
            <tag name="shopware.search.extension"/>
        </service>
    </services>
</container>

备选方案:用订阅器动态添加搜索逻辑

如果需要更灵活的控制,可以写一个订阅器类。在src/Subscriber/ProductSearchSubscriber.php中添加以下代码:

<?php

namespace YourPluginName\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Event\SearchCriteriaEvent;

class ProductSearchSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            SearchCriteriaEvent::class => 'onSearchCriteria'
        ];
    }

    public function onSearchCriteria(SearchCriteriaEvent $event): void
    {
        $entityName = $event->getDefinition()->getEntityName();
        // 只处理产品实体的搜索请求
        if ($entityName !== 'product') {
            return;
        }

        $searchTerm = $event->getSearchTerm();
        if (empty($searchTerm)) {
            return;
        }

        $criteria = $event->getCriteria();
        // 添加自定义字段的搜索过滤,替换成你的字段技术名
        $criteria->addFilter(new ContainsFilter('custom_field_your_field', $searchTerm));
    }
}

然后在services.xml里注册这个订阅器:

<service id="YourPluginName\Subscriber\ProductSearchSubscriber" public="true">
    <tag name="kernel.event_subscriber"/>
</service>

4. 测试验证

完成配置后,安装并激活你的自定义插件,然后在后台搜索框输入自定义字段的内容,就能找到对应的产品了。

注意事项

  • 把代码里的YourPluginName替换成你实际的插件名称
  • custom_field_your_field要换成你在后台创建的产品自定义字段的技术名称(可在自定义字段管理页面查看)
  • 如果自定义字段是关联类型、枚举类型等特殊类型,需要替换成对应的过滤器(比如EqualsFilter)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 19:15:48