I'm trying to figure out if there's a way to either treat product variants as ww.site.com/product/variant or pass variables from a variant list (for example thumbnails that click to product and passes along variables for the layout of that page). Any takers?

I'm assuming it would be better to create a category > sub-category > product. Just would be great to have the same product information but be able to point to .com/pillow/green whilst returning the same basic product information.

1 Answer 1


Put simply, if you define the url for your product type as, say,


...then if you visit this url (products/pillows/my-pretty-pillow) you would perhaps loop through and show all variants.

When they hit a url like products/pillows/my-pretty-pillow/green (to make this url work you'd need to define a route to point at the same template I think -> products/pillows/*/*)

....then you can simple look at craft.request.getSegment(4) and use that to determine which specific variant to show at that url.

(This is much the same question actually: How do I select a specific product variant by element/field/entry?)

  • For some crazy reason, I can't seem to get it to work. I can get it to where it loads the correct template in terms of routing, but I can't seem to get if/else to work with matching the sku to the slug. Structure-wise I have it set up as domain.com/products/pillow/ and would want to pass if/else statements to how it renders (variant selected and first image of a slideshow) based on if it is products/pillow/pillow-green (matching the sku) and fall back to the default product (and/or just redirect to the product page itself) if it doesn't exist...
    – Anders
    Oct 11, 2016 at 21:42
  • Well basically you'd use code like in the question I linked above....but paste what you've got (or find me on Slack) - happy to look it over Oct 11, 2016 at 23:59
  • So, at this point I'm just trying to spit out the sku based on the slug. It's ponting to segment 3 as it would be /products/pillow/sku...
    – Anders
    Oct 18, 2016 at 5:46
  • The code; {# Grab variant segment #} {% set variantSlug = craft.request.getSegment(3) %} {# If there is no variant, we grab the default variant #} {% if not variantSlug %} {# Grab default product variant #} {% set variantActive = product.defaultVariant %} {% else %} {# Set outside the loop #} {% set variantActive = '' %} {% for variantSelected in product.variants %} {% if variantSelected.sku == variantSlug %} {% set variantActive = variantSelected %} {% endif %} {% endfor %} {% if not variantActive %} {% exit 404 %} {% endif %} {% endif %}
    – Anders
    Oct 18, 2016 at 5:47
  • Maybe post a pastebin link to the code next time. In the meantime, confirm the basics - is product actually being set? If you simply loop through the variants, printing the skus, does that work? And check the value of the segment you retrieve is what you think it is... Oct 18, 2016 at 6:01

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.