1

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:

enter image description here

Any insight would be greatly appreciated.

1

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.

1
  • 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 Jun 10 at 18:48

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.