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

Shopware 6 SEO友好分页插件:添加隐藏href后出现报错

Shopware 6插件分页模板修改后报错排查与修复

我正在开发一款名为"SEO Friendly Pagination"的Shopware 6插件,目标是让产品分类的分页URL对搜索引擎爬虫更友好。思路是扩展分页模板,给每个分页页面添加包含排序、筛选等上下文信息的隐藏href元素。

自定义插件中的pagination.html.twig完整代码:

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

{% block component_pagination_loop %}
    {% set start = currentPage - 2 %}
    {% if start <= 0 %}
        {% set start = currentPage - 1 %}
        {% if start <= 0 %}
            {% set start = currentPage %}
        {% endif %}
    {% endif %}

    {% set end = start + 4 %}
    {% if end > totalPages %}
        {% set end = totalPages %}
    {% endif %}

    {% for page in start..end %}
        {% set isActive = (currentPage == page) %}

        {# Generate the SEO-friendly URL for the current pagination page #}
        {% set paginationUrlParams = {
            order: app.request.get('order'),
            properties: app.request.get('properties'),
            p: page
        } %}
        {% set seoFriendlyPaginationUrl = path('frontend.listing', paginationUrlParams) %}

        {# Add the invisible href element for the current page #}
        <a href="{{ seoFriendlyPaginationUrl }}" style="display: none;"></a>

        {% block component_pagination_item %}
            <li class="page-item{% if isActive %} active{% endif %}">
                {% block component_pagination_item_input %}
                    <input type="radio"
                           name="p"
                           id="p{{ page }}{{ paginationSuffix }}"
                           value="{{ page }}"
                           class="d-none"
                           title="pagination"
                           {% if isActive %}checked="checked"{% endif %}>
                {% endblock %}

                {% block component_pagination_item_label %}
                    <label class="page-link" for="p{{ page }}{{ paginationSuffix }}">
                        {% block component_pagination_item_link %}
                            {% block component_pagination_item_text %}
                                {{ page }}
                            {% endblock %}
                        {% endblock %}
                    </label>
                {% endblock %}
            </li>
        {% endblock %}
    {% endfor %}
{% endblock %}

添加上述隐藏href及URL生成逻辑后,本地Shopware 6环境弹出"Unfortunately, something went wrong"错误,排查后确定问题出在新增的代码片段里:

{# Generate the SEO-friendly URL for the current pagination page #}
{% set paginationUrlParams = {
    order: app.request.get('order'),
    properties: app.request.get('properties'),
    p: page
} %}
{% set seoFriendlyPaginationUrl = path('frontend.listing', paginationUrlParams) %}

{# Add the invisible href element for the current page #}
<a href="{{ seoFriendlyPaginationUrl }}" style="display: none;"></a>

错误原因

  1. app.request无法直接在模板中调用:Shopware 6的组件模板上下文里,app.request并未被正确注入,直接调用会导致变量未定义错误。
  2. frontend.listing路由缺少必填参数:该路由需要categoryId(或对应列表ID)才能生成有效URL,原代码只传入了order、properties和p,参数不全导致path()函数执行失败。

修复方案

替换URL生成逻辑,从模板上下文的page对象获取必要参数,并补充路由必填的分类ID:

{# Generate the SEO-friendly URL for the current pagination page #}
{% set activeProperties = page.productListing.facets|filter(facet => facet.active)|map(facet => facet.id)|join(',') %}
{% set paginationUrlParams = {
    categoryId: page.header.navigation.active.id,
    order: page.productListing.order,
    properties: activeProperties,
    p: page
} %}
{% set seoFriendlyPaginationUrl = path('frontend.listing', paginationUrlParams) %}

{# 用Shopware自带的视觉隐藏类替代display:none,兼顾爬虫识别和视觉隐藏 #}
<a href="{{ seoFriendlyPaginationUrl }}" class="visually-hidden"></a>

额外优化说明

  • 使用visually-hidden类替代display:none:搜索引擎可能忽略display:none的元素,而visually-hidden是Shopware内置的辅助类,仅在视觉上隐藏元素,爬虫仍能读取到href内容。
  • 通过page.productListing获取排序和筛选参数:这是Shopware模板中获取列表上下文的标准方式,比直接调用请求更可靠。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 11:17:13