How can a Shopware SEO URL template handle an empty product field?

The Citead team, updated

In short. Guard optional Shopware product fields with Twig conditions before adding their URL segments.

Definition: In a Shopware SEO URL template, put an optional product path segment inside a Twig if condition. Use is defined when the product attribute may be unavailable, and choose a null or empty test according to the value you need to accept.

Key facts

  • Shopware's SEO settings define the structure of product detail page and category URLs with variables in templates, including Twig expressions (Shopware SEO settings).
  • Shopware documents that a selected variable can be unavailable for some products and recommends protecting the SEO template with an IF query.
  • Its product example appends canonicalProductId only when that field has a value.
  • Twig's defined test checks whether a variable exists in the current context (Twig defined test).
  • Twig's empty test matches an empty string, an empty sequence or mapping, false, and null (Twig empty test).

How should the condition be written?

Shopware's example places the optional slash and field output inside the condition. When canonicalProductId is null, the conditional block produces no path segment.

{{ product.translated.name }}/{{ product.productNumber }}{% if product.canonicalProductId is not null %}/{{ product.canonicalProductId }}{% endif %}

The same Shopware documentation says that the default product template remains in use and that the additional content is added when canonicalProductId is filled. This form suits a field that is available on the product object but can have a null value. Twig documents the null test and its true result when a variable is null (Twig null test).

What if the product attribute is unavailable?

An unavailable attribute needs a definition test before its value is used. Twig documents is defined for variables and attributes, including an example with user.name. A condition for an optional product field can therefore be written as follows:

{% if product.canonicalProductId is defined and product.canonicalProductId is not empty %}
/{{ product.canonicalProductId }}
{% endif %}

Here, is defined checks that the attribute exists in the current context. is not empty then requires a value outside the empty set covered by Twig's empty test, including an empty string, an empty sequence or mapping, false, and null. For a null-only guard, the shorter null comparison matches Shopware's documented example.

How does Shopware check the template?

Shopware shows a validity indicator beside the product URL template. The documentation says that a green tick means the variables are correct. A red X means that at least one variable fails to resolve, and the entry needs adjustment before the settings can be saved.

This check is useful for catching a misspelled or unavailable variable, while the conditional expression controls whether an optional value contributes a segment to the generated path. The condition must include the separator, such as /, so an absent value leaves no extra separator in the template output.

What must be rebuilt after the change?

After an SEO template change, Shopware says that the SEO index must be rebuilt so that the URLs are updated. Its documented console command is:

php bin/console dal:refresh:index

The command belongs to the URL update step after saving the revised template.

FAQ

Should I use defined or a null test?

Use is defined when the attribute itself may be unavailable. Use the null comparison when the attribute is available and the optional case documented by Shopware is a null value. For empty strings and the other values covered by Twig's empty test, use the empty test.

Should the slash be inside the conditional block?

Yes. Shopware's example places /{{ product.canonicalProductId }} inside the if block, so the separator and the value are emitted together only when the condition passes.

What should I run after editing the template?

Rebuild the SEO index with php bin/console dal:refresh:index, the command given in Shopware's SEO settings documentation after a template change.

Sources

  1. Shopware, "SEO-settings", undated
  2. Twig, "defined", undated
  3. Twig, "empty", undated
  4. Twig, "null", undated

Check how a page reads to answer engines. Open the GEO score analyzer

Test a page and see what to fix first

The analysis usually takes less than 10 seconds and needs no account.

400,000+ pages scored in the public benchmark