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

Shopware 6自定义SELECT字段下拉选项为空问题求助

Shopware 6自定义SELECT字段下拉选项显示为空的解决办法

问题现象

自定义SELECT字段已成功创建并关联到分类实体,但在后台分类设置页面中,该字段的下拉列表显示两个空选项,而非预设的「ON」和「OFF」。

原因分析

  1. 选项标签缺失系统默认语言配置:代码中仅为选项标签设置了en-GB和de-DE语言,未添加Shopware的系统默认语言(Defaults::LANGUAGE_SYSTEM)。后台渲染时,若当前语言无匹配标签,会 fallback 到系统默认语言,缺失则显示为空。
  2. 缓存未更新:自定义字段创建后,Shopware缓存未及时清理,后台无法加载最新的字段配置。
  3. 字段创建逻辑存在重复风险:在activate方法中创建字段,插件每次激活都会执行,可能导致重复创建或配置冲突。

解决方案

1. 补充系统默认语言标签

为每个选项的label添加Defaults::LANGUAGE_SYSTEM对应的配置,确保所有场景下都能加载到标签文本。

2. 优化字段创建逻辑

将字段创建逻辑移至install方法,并先检查字段集是否已存在,避免重复创建。

3. 清理缓存

字段创建后自动清理缓存,或手动在后台清理。

修改后的完整代码

<?php declare(strict_types=1);

namespace MyNameSpace;

use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

class MyClass extends Plugin
{
    public function install(InstallContext $installContext): void
    {
        parent::install($installContext);
        
        $context = $installContext->getContext();
        $this->createCustomField($context);
    }

    private function createCustomField(Context $context): void
    {
        $customFieldSetRepository = $this->container->get('custom_field_set.repository');
        
        // 检查字段集是否已存在,避免重复创建
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('name', 'categoryNoteSet'));
        $existingSet = $customFieldSetRepository->search($criteria, $context)->first();
        
        if ($existingSet) {
            return;
        }

        $customFieldData = [
            'name' => 'categoryNoteSet',
            'config' => [
                'label' => [
                    'en-GB' => 'Categories Notes',
                    'de-DE' => 'Kategorien Hinweise',
                    Defaults::LANGUAGE_SYSTEM => "Categories Notes"
                ]
            ],
            'relations' => [
                [
                   'entityName' => 'category'
                ]
            ],
            'customFields' => [
                // ... 其他自定义字段
                [
                    'name' => 'noteOfferActivate',
                    'type' => CustomFieldTypes::SELECT,
                    'config' => [
                        'label' => [
                            'en-GB' => 'Activate Offer note',
                            'de-DE' => 'Angebot Hinweis aktivieren',
                            Defaults::LANGUAGE_SYSTEM => "Activate Offer note"
                        ],
                        'options' => [
                            [
                                'label' => [
                                    'en-GB' => 'ON',
                                    'de-DE' => 'An',
                                    Defaults::LANGUAGE_SYSTEM => 'ON'
                                ],
                                'value' => 'true',
                            ],
                            [
                                'label' => [
                                    'en-GB' => 'OFF',
                                    'de-DE' => 'Aus',
                                    Defaults::LANGUAGE_SYSTEM => 'OFF'
                                ],
                                'value' => 'false',
                            ]               
                        ],
                        'customFieldPosition' => 5
                    ]
                ],
                // ... 其他自定义字段
            ]
        ];

        $customFieldSetRepository->create([$customFieldData], $context);
        
        // 自动清理缓存
        $cacheClearer = $this->container->get('cache_clearer');
        $cacheClearer->clear();
    }
}

后续操作

  1. 卸载并重新安装插件,或执行bin/console plugin:install MyPlugin --activate命令
  2. 若自动缓存清理无效,手动进入后台「设置 > 系统 > 缓存管理」清理所有缓存
  3. 刷新分类设置页面,验证下拉选项是否正常显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 13:07:49