Shopware 6插件事件订阅器未触发:无product.search.criteria监听器注册
问题背景
我正在开发一款Shopware 6插件,目标是通过自定义字段custom_product_ranking对商品搜索结果进行排序。实现了事件订阅器,监听ProductSearchCriteriaEvent和product.suggest.criteria事件以修改搜索条件,但订阅器始终未触发。
已排查情况
- 未出现订阅器的日志信息,也未生成相关调试日志
- 执行
bin/console debug:event-dispatcher product.search.criteria显示无已注册监听器 - 插件已安装并激活
services.xml已配置订阅器为kernel.event_subscriber- 已多次清除缓存
- 插件主类无手动加载服务的
build()方法 - 命名空间和目录结构符合PSR-4标准
- 在订阅器事件处理程序中手动添加异常,未显示错误,确认订阅器未被调用
相关代码片段
services.xml(路径:src/Resources/config/services.xml)
<?xml version="1.0" ?> <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="MSX\Subscriber\SearchSortingSubscriber" public="true"> <argument type="service" id="Psr\Log\LoggerInterface" /> <tag name="kernel.event_subscriber" /> </service> </services> </container>
订阅器类
<?php declare(strict_types=1); namespace MSX\Subscriber; use Psr\Log\LoggerInterface; use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchCriteriaEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting; class SearchSortingSubscriber implements EventSubscriberInterface { private LoggerInterface $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public static function getSubscribedEvents(): array { return [ ProductSearchCriteriaEvent::class => 'onSearch', 'product.suggest.criteria' => 'onSuggestSearch', ]; } public function onSearch(ProductSearchCriteriaEvent $event): void { $this->logger->info('Search event triggered'); $criteria = $event->getCriteria(); $criteria->resetSorting(); $criteria->addSorting(new FieldSorting('customFields.custom_product_ranking', FieldSorting::DESCENDING)); } public function onSuggestSearch(EntitySearchCriteriaEvent $event): void { $this->logger->info('Suggest search event triggered'); $criteria = $event->getCriteria(); $criteria->resetSorting(); $criteria->addSorting(new FieldSorting('customFields.custom_product_ranking', FieldSorting::DESCENDING)); } }
待解决问题
为什么我的订阅器在Shopware 6中未注册且未触发?还遗漏了什么或可以检查哪些内容来修复问题?
排查方向与解决方案
验证插件主类配置
确认插件主类(如MSX.php)位于src/目录下,命名空间为namespace MSX;,且类名与文件名完全一致。Shopware依赖此识别插件根命名空间,否则服务无法正确加载。检查composer.json自动加载配置
确保composer.json的autoload块正确配置PSR-4映射:"autoload": { "psr-4": { "MSX\\": "src/" } }配置后执行
composer dump-autoload更新自动加载规则。核对服务ID与类的完全限定名
确认services.xml中的服务IDMSX\Subscriber\SearchSortingSubscriber与订阅器类的完全限定名完全一致,Linux环境下需注意文件名和类名的大小写敏感问题。验证事件名称有效性
- 执行
bin/console debug:event-dispatcher查看所有可用事件,确认ProductSearchCriteriaEvent::class对应的事件名是否为product.search.criteria,以及product.suggest.criteria事件是否存在。 - 若事件名称不匹配,需修正
getSubscribedEvents()中的事件映射。
- 执行
检查服务是否被容器加载
执行bin/console debug:container MSX\Subscriber\SearchSortingSubscriber,若服务未找到,说明services.xml路径错误(必须为src/Resources/config/services.xml)或存在其他配置覆盖了服务注册。确认缓存与环境状态
- 开发环境下测试,生产环境需执行
bin/console cache:clear --env=prod彻底清除缓存,必要时手动删除var/cache目录。 - 切换环境后需重新激活插件。
- 开发环境下测试,生产环境需执行
检查插件激活状态与依赖
执行bin/console plugin:list确认插件状态为Active,若存在依赖缺失,需先安装并激活依赖插件。
内容的提问来源于stack exchange,提问作者mingus2

