shopwareshopware6shopware6-appshopware6-apishopware5

Shopware 6 Plugin - SEO Pagination: Parameter "navigationId" for route "frontend.navigation.page" must match "[^/] after adding invisible href element


I'm currently developing a Shopware 6 plugin called "SEO Friendly Pagination" that aims to make paginated URLs in product categories accessible to search engine crawlers. To achieve this, I have extended the pagination template to include invisible href elements for each page in the pagination. These href elements are generated with SEO-friendly URLs that reflect the current context of the page, including sorting order and filters.

The pagination template in my custom plugin pagination.html.twig looks like this:

{% 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 %}

However, after adding the invisible href elements and the URL generation logic, I do not see any desired results. So, I found out the if condition of line " {% if page.header.navigation.active is defined %}" is return false so thats why I removed this line and I encounter an error "Parameter "navigationId" for route "frontend.navigation.page" must match "[^/]++" ("" given) to generate a corresponding URL".

page.header.navigation.active is not defined and navigationId parameter is not being set correctly in the seoUrl() function call. I also added dump command for currentCategoryId. But I could not see any dumped values on my localhost. How can I obtain the correct value for the navigationId parameter?

Thank you in advance for your assistance!

Best regards, Lodhi


Solution

  • Use searchResult.currentFilters.navigationId as an alternative.

    {% if searchResult.currentFilters.navigationId is defined %}
        {% set currentNavigationId = searchResult.currentFilters.navigationId %}
    {% endif %}
    {% if page.header.navigation.active.id is defined %}
        {% set currentNavigationId = page.header.navigation.active.id %}
    {% endif %}
    {% if currentNavigationId is defined %}
        <a href="{{ seoUrl('frontend.navigation.page', { navigationId: currentNavigationId }) }}" 
           style="display: none;"></a>
    {% endif %}