1

I have a website that uses Craft Commerce and I'm trying to add a language menu that points to the localised version of each product variant as follows:

  {% set variantID = craft.request.getParam('variant') %}
  {% set locales = craft.i18n.getSiteLocales() %}

  {% for locale in locales %}
     {% set localisedEntryUrl = craft.commerce.variants.id(variantID).locale(locale.id).first().url() %}
     {{ localisedEntryUrl }}
  {% endfor %}

The problem is that the code above always produces the variant url for one language. eg.

/en/shop/ornaments/melisa?variant=9356
/en/shop/ornaments/melisa?variant=9356
/en/shop/ornaments/melisa?variant=9356

whereas I would expect something like:

/en/shop/ornaments/melisa?variant=9356
/de/shop/ornaments/melisa?variant=9356
/es/shop/ornaments/melisa?variant=9356

What am I doing wrong?

1

Try using this code:

{% set variantID = craft.request.getParam('variant') %}
{% set locales = craft.i18n.getSiteLocaleIds() %}

{% for locale in locales %}
    {% if variantID is defined %}
        {% set localisedEntryUrl = craft.commerce.variants.id(variantID).locale(locale).first %}
        {% if localeEntry.locale == locale %}
            {{ localisedEntryUrl.getUrl() }}
        {% endif %}
    {% endif %}
{% endfor %}
  • Thank you for taking the time to respond! Unfortunately, this code produces the error: Variable "localeEntry" does not exist. If you meant entry.locale I tried it and it didn't work. Still produces the same link for all locales. – Panos Spiliotis May 9 '18 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.