<p>Put simply, if you define the url for your product type as, say,</p>

<p><code>products/{categories.first().slug}/{slug}</code></p>

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

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

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

<p>(This is  much the same question actually:
<a href="https://craftcms.stackexchange.com/questions/12708/how-do-i-select-a-specific-product-variant-by-element-field-entry">How do I select a specific product variant by element/field/entry?</a>)</p>

CC BY-SA 3.0
2016-10-11T21:42:43.123Z
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
CC BY-SA 3.0
2016-10-11T23:59:13.200Z
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
Jeremy Daalder
CC BY-SA 3.0
2016-10-18T05:46:20.190Z
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
CC BY-SA 3.0
2016-10-18T05:47:06.240Z
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
CC BY-SA 3.0
2016-10-18T06:01:32.757Z
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...
Jeremy Daalder
CC BY-SA 3.0
2016-10-18T06:10:20.723Z
Sorry about that, didn't realize one couldn't post formatted code, or edit comments. Products and everything works, everything from asset fields to color fields and so forth.
Anders
CC BY-SA 3.0
2016-10-18T06:13:21.373Z
Meet me on slack in #commerce?  be quicker.  But basically, in your code, if no match occurs, you 404 - is that actually happening?  If not - then it's probably just you need to move the `{% set variantActive = '' %} ` outside of the if to get it properly in scope to the rest of the template.  If it does 404 then you need to look at why a match is not occurring...
Jeremy Daalder
CC BY-SA 3.0
2016-10-18T06:16:10.177Z
https://kobra.io/#/e/-KULQLzm2DnSew_fxhlJ - slightly updated code
Jeremy Daalder
CC BY-SA 3.0
2016-10-18T06:20:31.863Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/46960/discussion-between-jeremy-daalder-and-anders).
Jeremy Daalder
CC BY-SA 3.0
2016-11-17T22:18:26.890Z
Solved it, and the issue was scope, had to set the variable outside the loop to be able to fetch inside different for loops. It's usually simple like that. ;)
Anders
CC BY-SA 3.0
2016-10-01T04:59:42.540Z
2016-10-01T04:59:42.540Z
2017-04-13T12:51:00.303Z
Jeremy Daalder
<p>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?</p>

<p>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.</p>

<plugin-craftcommerce>
Is there a way to treat variants in a similar way as products?
# Is there a way to treat variants in a similar way as products?

(Asked by: username_0 on 2016-10-01)

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.

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2016-10-01. Score: 2 | Accepted answer

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

`products/{categories.first().slug}/{slug}`

...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?](https://craftcms.stackexchange.com/questions/12708/how-do-i-select-a-specific-product-variant-by-element-field-entry))

> **Comments:**
> **username_0**: 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...  
> **username_1**: 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  
> **username_0**: 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...  
> **username_0**: 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 %}`  
> **username_1**: 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...  
> **username_0**: Sorry about that, didn't realize one couldn't post formatted code, or edit comments. Products and everything works, everything from asset fields to color fields and so forth.  
> **username_1**: Meet me on slack in #commerce?  be quicker.  But basically, in your code, if no match occurs, you 404 - is that actually happening?  If not - then it's probably just you need to move the `{% set variantActive = '' %} ` outside of the if to get it properly in scope to the rest of the template.  If it does 404 then you need to look at why a match is not occurring...  
> **username_1**: https://kobra.io/#/e/-KULQLzm2DnSew_fxhlJ - slightly updated code  
> **username_1**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/46960/discussion-between-jeremy-daalder-and-anders).  
> **username_0**: Solved it, and the issue was scope, had to set the variable outside the loop to be able to fetch inside different for loops. It's usually simple like that. ;)  

---

