Build the URL for a Product Variant

If you have a product with multiple variants, (a product that comes in different colour, for example) you can use this code to generate the URL for each variant.

{% set url = "/product/#{product.url_name}?" %}
{% for variant in product.productVariants %} 
  {% for key, value in variant.getOptionsArray %}
    {% set url = url ~ "options[#{key}]=#{value}" %}
  {% endfor %}
{% endfor %}