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
2
Answers
Use
searchResult.currentFilters.navigationId
as an alternative.This is probably best created as part of your child theme as it may be theme-specific, but a plugin would also work. Don’t create invisible elements though – do it right.
You need to re-implement pagination properly, using
a
elements instead of the brokenbutton
based navigation. I also recommend not using canonical on subpages, unlike what you might see some do, because the pages indeed have different content. The actual results can be said to make up the content.In our case I simply carefully created my own blocks for the following:
component_pagination_prev
component_pagination_item
component_pagination_next
component_pagination_last
1. You need to be aware that the components may be used both for search results and for pagination, and you may need to account for that in your
twig
– I already did in the following code.2. You should also account for use of filters. Filters are used for sorting, and will be included in the URL as parameters, so you need to re-build the query string. I also did that already in the following example.
This file simply extends Shopware’s own
pagination.html.twig
:It should probably be located in:
Remember to translate relevant parts in your snippets. E.g. the "Show all" text.