I'm facing an issue with generating SEO-friendly URLs in Shopware 6 when implementing pagination for search results. I have a custom Twig template that extends the pagination.html.twig template from the storefront. The code works perfectly for generating SEO URLs in categories, but I'm encountering problems when navigating to page 2 (from page 1) from a search results page.
The issue seems to revolve around how I'm modifying the seoParams and handling the currentNavigationId for search result pages. Here's a simplified version of the code
{# Extend the 'pagination.html.twig' template from the storefront #}
{% sw_extends '@Storefront/storefront/component/pagination.html.twig' %}
{# Define the 'component_pagination' block #}
{% block component_pagination %}
{# Loop through each page #}
{% for i in 1..totalPages %}
{# Set up an empty SEO parameters object #}
{% set seoParams = {} %}
{# Retrieve and modify query parameters #}
{% set seoParams = context.context.extensions.tanmarNgSeoPagination.queryParameters %}
{# Check if the current page is not equal to the current iteration #}
{% if i != currentPage %}
{% if currentPage %}
{% set seoParams = seoParams|merge({ p: i }) %}
{% endif %}
{# Properly encode the SEO parameters into a string before using it #}
{% set seoParams = '?' ~ seoParams|url_encode %}
{# Check if currentNavigationId is defined and generate SEO-friendly URL #}
{% if currentNavigationId is defined %}
{{ dump(seoParams) }}
<a href="{{ seoUrl('frontend.navigation.page', { navigationId: currentNavigationId }) ~ seoParams }}"></a>
{% endif %}
{% endif %}
{% endfor %}
{# Call the parent pagination block #}
{{ parent() }}
{% endblock %}
I have checked the URL encoding, seoUrl function, and the overall logic, but I can't seem to figure out why the SEO URLs are not being generated correctly when navigating to page 2 from a search results page. Any insights or suggestions would be greatly appreciated.
I have noticed one thing. When I search for something the link of the browser changes such that it removes "/Clothing/Men/" from the link http://localhost/Clothing/Men/ and change it to http://localhost/search?search=new
Thank you in advance for your help!
The search page uses a different route than the navigation pages. It also doesn't take a navigationId
as a parameter, as it is not associated with one specific category for instance. You can check whether the page has a current searchTerm
property and, if so, generate a URL for the correct frontend.search.page
route name. While you do that, you can also add the searchTerm
to the rest of your parameters, as you'll want further URLs in your pagination to continue using that search term.
{% if page.searchTerm is defined %}
{% set seoUrl = seoUrl('frontend.search.page') %}
{% set seoParams = seoParams|merge({ search: page.searchTerm }) %}
{% else %}
{% set seoUrl = seoUrl('frontend.navigation.page', { navigationId: currentNavigationId }) %}
{% endif %}
{% set seoParams = '?' ~ seoParams|url_encode %}
<a href="{{ seoUrl ~ seoParams }}"></a>