1

I noticed that variants can have their own URL when i outputted URL of line item in the cart. Url looks something like that:

http://localhost/test/web/test-prod?variant=27

Is there any way to change variant keyword in URL to something else?

When we want to display specific variant on product page, we need to perform specific element query based on variant param ourselves, yes? Because only product variable is injected into template.

1

Not sure how you're outputting the url to the variant itself, but you can always create your own link and change the variant paramater to whatever you want.

Something link this: {{product.url}}?keywordyouwanttouse={{variant.purchasableId}}

On your product page you can then check if the url has the parameter and get the data of that variant.

I use the following code at the top of my product template and then use the selectedVariantto display sku, images, prices, ... Here I use the variant querystring but you can change that to whatever querystring paramater you want to use.

{% set variantId = craft.app.request.getQueryParam('variant') ?? false %}
{%  if variantId %}
    {% set selectedVariant = craft.variants.id(variantId).one() %}
{% else %}
    {% set selectedVariant = product.defaultVariant %}
{% endif %}
1
  • If you output url attribute of lineitem from cart, it links to specific variant, using keyword variant.
    – piotrpog
    Sep 24 '20 at 14:18

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.