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

Shopware热销排序下商品列表分页重复显示问题求助

Shopware热销(Top Seller)排序分页重复问题解决方法

问题原因

热销排序默认仅按商品sales(销量)字段降序排列,当多个商品(尤其是共享父商品销量的变体)销量相同时,数据库返回的结果顺序不稳定,导致分页时同一商品可能出现在不同页面。

解决步骤

针对Shopware 6(推荐用自定义插件修改,避免核心更新覆盖)

  1. 创建自定义插件
    • 新建插件目录(如CustomTopSellerSort),按Shopware 6插件规范搭建基础结构。
  2. 添加事件订阅器
    • 在插件src/Subscriber目录下创建ProductListingSubscriber.php文件,代码如下:
      <?php
      
      namespace CustomTopSellerSort\Subscriber;
      
      use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingCriteriaEvent;
      use Symfony\Component\EventDispatcher\EventSubscriberInterface;
      
      class ProductListingSubscriber implements EventSubscriberInterface
      {
          public static function getSubscribedEvents(): array
          {
              return [
                  ProductListingCriteriaEvent::class => 'onProductListingCriteria'
              ];
          }
      
          public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
          {
              $criteria = $event->getCriteria();
              $sorting = $criteria->getSorting();
      
              // 检测当前是否为热销排序
              if ($sorting && $sorting->getField() === 'sales' && $sorting->getDirection() === 'DESC') {
                  // 添加商品ID作为第二排序字段,保证顺序稳定
                  $criteria->addSorting(
                      new \Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting(
                          'product.id',
                          \Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting::ASCENDING
                      )
                  );
              }
          }
      }
      
  3. 注册订阅器
    • 在插件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="CustomTopSellerSort\Subscriber\ProductListingSubscriber">
                  <tag name="kernel.event_subscriber"/>
              </service>
          </services>
      </container>
      
  4. 安装插件并测试
    • 在Shopware后台安装该插件,清除缓存后,测试热销排序的分页,确认商品不再重复出现。

针对Shopware 5

  1. 插件方式(推荐,避免核心更新覆盖)
    • 创建插件并监听Enlight_Controller_Action_PostDispatchSecure_Frontend_Listing事件,在事件回调中修改商品列表的排序规则,在销量降序后添加商品ID升序排序。
  2. 临时核心修改方案
    • 打开engine/Shopware/Controllers/Frontend/Listing.php,找到indexAction中处理排序的代码块,在按sales排序后追加ID排序:
      // 找到类似代码
      $this->list->setSorting('sales', 'desc');
      // 添加以下代码
      $this->list->addOrderBy('s_articles.id', 'ASC');
      
    • 注意:此方法会在核心版本更新时被覆盖,仅作为临时应急方案。

验证方法

  1. 进入“Demo category 1”分类,选择热销排序。
  2. 依次浏览各分页,检查“Demo 1”等变体商品是否不再重复出现在不同页面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 13:42:39