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

Shopware 6 SEO分页插件:如何获取正确的navigationId参数?

问题:Shopware 6插件中获取分类ID用于SEO分页URL生成

问题背景

我正在开发一款名为“SEO Friendly Pagination”的Shopware 6插件,目的是让商品分类的分页URL能被搜索引擎爬虫抓取。为此我扩展了分页模板,给每个分页添加隐藏的href元素,这些元素的URL包含排序、筛选等当前页面的上下文信息。

现有代码

自定义插件分页模板pagination.html.twig代码如下:

{% sw_extends '@Storefront/storefront/component/pagination.html.twig' %}

{% set configService = sw.service('TanmarSeoPagination.config_service') %}

{% block component_pagination_item %}
{#        {% if configService.isPluginEnabled() %}#}
           {% if page.header.navigation.active is defined %}
                {% set currentCategoryId = page.header.navigation.active.id %}
                {# Debug output to check the currentCategoryId variable #}
                 {% dump(currentCategoryId + "here") %}
                 
                {% set totalPages = page.totalPages %}
                {% for i in 1..totalPages %}
                    <a href="{{ seoUrl('frontend.navigation.page', { navigationId: currentCategoryId }) }}?p={{ i }}" 
                      style="display: none;
                    ">
                    </a>
                {% endfor %}
            {% endif %}
{#        {% endif %}#}
         {{ parent() }}
{% endblock %}

遇到的问题

添加逻辑后未达预期:

  • {% if page.header.navigation.active is defined %}条件不成立,dump命令无输出
  • 移除条件判断后,抛出错误:Parameter "navigationId" for route "frontend.navigation.page" must match "[^/]++" ("" given) to generate a corresponding URL

核心问题是无法正确获取当前分类的navigationId,导致SEO URL生成失败。

解决方案

在Shopware 6的分页模板中,page对象并不直接包含header.navigation.active,可通过以下两种方式获取正确的分类ID:

方法1:从路由参数中提取

分页组件渲染时,当前路由参数会传入模板的context对象,直接从中获取navigationId:

{% set currentCategoryId = context.route.parameters.navigationId ?? null %}

方法2:从页面元数据中获取

如果当前是分类页,页面meta信息会包含分类数据,可通过以下方式提取ID:

{% set currentCategory = page.meta.navigation %}
{% set currentCategoryId = currentCategory.id ?? null %}

修改后的完整代码示例

{% sw_extends '@Storefront/storefront/component/pagination.html.twig' %}

{% set configService = sw.service('TanmarSeoPagination.config_service') %}

{% block component_pagination_item %}
{#        {% if configService.isPluginEnabled() %}#}
           {% set currentCategoryId = context.route.parameters.navigationId ?? page.meta.navigation.id ?? null %}
           {% if currentCategoryId %}
                {% set totalPages = page.totalPages %}
                {% for i in 1..totalPages %}
                    {# 保留当前页面的筛选、排序参数 #}
                    {% set queryParams = app.request.query.all %}
                    {% set queryParams = queryParams|merge({'p': i}) %}
                    
                    <a href="{{ seoUrl('frontend.navigation.page', { navigationId: currentCategoryId }, queryParams) }}" 
                       style="display: none;">
                    </a>
                {% endfor %}
            {% endif %}
{#        {% endif %}#}
         {{ parent() }}
{% endblock %}

额外优化:加入queryParams保留当前页面的筛选、排序参数,确保生成的分页URL包含完整上下文信息,更贴合SEO需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 23:22:23