<p>I can't really follow what you're trying to do with that twig, to be honest (I think you've got a bit lost in the Commerce Product/variant/purchasabale language, which is unfortunately easy to do!)....</p>
<p>...But if you're just wanting to display the SKU of a product, then try this:</p>
<p>Assuming a valid <code>productId</code>....</p>
<p>For a product without variants:</p>
<pre><code>{% set product = craft.products.id(productId).one() %}
{% set variant = product.defaultVariant %}
{{ variant.sku }}
</code></pre>
<p>For a product with variants:</p>
<pre><code>{% set product = craft.products.id(productId).one() %}
{% set variants = product.variants %}
{% for variant in variants %}
   {{ variant.sku }}
{% endfor %}
</code></pre>
<p>...that should do it.</p>

CC BY-SA 4.0
2021-06-10T18:48:04.963Z
Thank you Jeremy! I could not figure this out. Based on your answer I was able to switch this line:

`{% set variants = craft.variants().productID(product.id) %} `

For:


`{% set variants = product.variants %}`


And this then allowed me to access `{{ variant.sku }}`
Bmk
CC BY-SA 4.0
2021-06-09T09:08:23.190Z
2021-06-10T08:36:17.147Z
2021-06-10T08:36:17.147Z
Jeremy Daalder
<p>I am unable to access a variants sku.</p>
<pre><code>{% set product = craft.products.id(productId).with('productImages').one() %}
{% set defaultVariant = product.defaultVariant %}
{% set variants = craft.variants().productID(product.id) %}    
{% set purchasable = purchasableId ?? defaultVariant.id %}
{% set myVariantQuery = craft.variants.id(purchasable) ?? '' %}
{% set selectedVariant = myVariantQuery.one() %}
</code></pre>
<p>This is the set up and from what I can see in the docs I should be able now do the following in Twig:</p>
<pre><code>{{ selectedVariant.sku }}
</code></pre>
<p>But this does not work.</p>
<p>This is what is available:</p>
<pre><code>{
  &quot;productId&quot;: &quot;183&quot;,
  &quot;isDefault&quot;: &quot;0&quot;,
  &quot;price&quot;: &quot;4.9500&quot;,
  &quot;sortOrder&quot;: &quot;2&quot;,
  &quot;width&quot;: null,
  &quot;height&quot;: null,
  &quot;length&quot;: null,
  &quot;weight&quot;: null,
  &quot;stock&quot;: &quot;0&quot;,
  &quot;hasUnlimitedStock&quot;: &quot;0&quot;,
  &quot;minQty&quot;: null,
  &quot;maxQty&quot;: null,
  &quot;deletedWithProduct&quot;: false,
  &quot;id&quot;: 185,
  &quot;tempId&quot;: null,
  &quot;draftId&quot;: null,
  &quot;revisionId&quot;: null,
  &quot;uid&quot;: &quot;5dd47b96-2635-4a85-bdf9-e2850efe4f71&quot;,
  &quot;siteSettingsId&quot;: 185,
  &quot;fieldLayoutId&quot;: 39,
  &quot;structureId&quot;: null,
  &quot;contentId&quot;: 176,
  &quot;enabled&quot;: true,
  &quot;archived&quot;: false,
  &quot;siteId&quot;: 1,
  &quot;title&quot;: &quot;250g&quot;,
  &quot;slug&quot;: null,
  &quot;uri&quot;: null,
  &quot;dateCreated&quot;: {
    &quot;date&quot;: &quot;2021-03-01 07:14:11.000000&quot;,
    &quot;timezone_type&quot;: 3,
    &quot;timezone&quot;: &quot;America/Los_Angeles&quot;
  },
  &quot;dateUpdated&quot;: {
    &quot;date&quot;: &quot;2021-05-28 12:59:47.000000&quot;,
    &quot;timezone_type&quot;: 3,
    &quot;timezone&quot;: &quot;America/Los_Angeles&quot;
  },
  &quot;dateDeleted&quot;: null,
  &quot;root&quot;: null,
  &quot;lft&quot;: null,
  &quot;rgt&quot;: null,
  &quot;level&quot;: null,
  &quot;searchScore&quot;: null,
  &quot;trashed&quot;: false,
  &quot;awaitingFieldValues&quot;: false,
  &quot;propagating&quot;: false,
  &quot;propagateAll&quot;: false,
  &quot;newSiteIds&quot;: [],
  &quot;resaving&quot;: false,
  &quot;duplicateOf&quot;: null,
  &quot;previewing&quot;: false,
  &quot;hardDelete&quot;: false
}
</code></pre>
<p>The product has a SKU in admin:</p>
<p><a href="https://i.sstatic.net/WwFpw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WwFpw.png" alt="enter image description here" /></a></p>
<p>Any insight would be greatly appreciated.</p>

<plugin-craftcommerce>
Variant SKU not accessible in Twig
# Variant SKU not accessible in Twig

(Asked by: username_0 on 2021-06-08)

I am unable to access a variants sku.
{% set product = craft.products.id(productId).with('productImages').one() %}
{% set defaultVariant = product.defaultVariant %}
{% set variants = craft.variants().productID(product.id) %}    
{% set purchasable = purchasableId ?? defaultVariant.id %}
{% set myVariantQuery = craft.variants.id(purchasable) ?? '' %}
{% set selectedVariant = myVariantQuery.one() %}

This is the set up and from what I can see in the docs I should be able now do the following in Twig:
{{ selectedVariant.sku }}

But this does not work.
This is what is available:
{
  "productId": "183",
  "isDefault": "0",
  "price": "4.9500",
  "sortOrder": "2",
  "width": null,
  "height": null,
  "length": null,
  "weight": null,
  "stock": "0",
  "hasUnlimitedStock": "0",
  "minQty": null,
  "maxQty": null,
  "deletedWithProduct": false,
  "id": 185,
  "tempId": null,
  "draftId": null,
  "revisionId": null,
  "uid": "5dd47b96-2635-4a85-bdf9-e2850efe4f71",
  "siteSettingsId": 185,
  "fieldLayoutId": 39,
  "structureId": null,
  "contentId": 176,
  "enabled": true,
  "archived": false,
  "siteId": 1,
  "title": "250g",
  "slug": null,
  "uri": null,
  "dateCreated": {
    "date": "2021-03-01 07:14:11.000000",
    "timezone_type": 3,
    "timezone": "America/Los_Angeles"
  },
  "dateUpdated": {
    "date": "2021-05-28 12:59:47.000000",
    "timezone_type": 3,
    "timezone": "America/Los_Angeles"
  },
  "dateDeleted": null,
  "root": null,
  "lft": null,
  "rgt": null,
  "level": null,
  "searchScore": null,
  "trashed": false,
  "awaitingFieldValues": false,
  "propagating": false,
  "propagateAll": false,
  "newSiteIds": [],
  "resaving": false,
  "duplicateOf": null,
  "previewing": false,
  "hardDelete": false
}

The product has a SKU in admin:

Any insight would be greatly appreciated.

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2021-06-09. Score: 1 | Accepted answer

I can't really follow what you're trying to do with that twig, to be honest (I think you've got a bit lost in the Commerce Product/variant/purchasabale language, which is unfortunately easy to do!)....
...But if you're just wanting to display the SKU of a product, then try this:
Assuming a valid productId....
For a product without variants:
{% set product = craft.products.id(productId).one() %}
{% set variant = product.defaultVariant %}
{{ variant.sku }}

For a product with variants:
{% set product = craft.products.id(productId).one() %}
{% set variants = product.variants %}
{% for variant in variants %}
   {{ variant.sku }}
{% endfor %}

...that should do it.

> **Comments:**
> **username_0**: Thank you Jeremy! I could not figure this out. Based on your answer I was able to switch this line:  `{% set variants = craft.variants().productID(product.id) %} `  For:   `{% set variants = product.variants %}`   And this then allowed me to access `{{ variant.sku }}`  

---

