<p>It's a little awkward, but the documentation has a note about this: <a href="https://craftcommerce.com/docs/product-types#automatic-sku-format" rel="nofollow noreferrer">https://craftcommerce.com/docs/product-types#automatic-sku-format</a></p>

<p>If a product does <em>not</em> have a variant, then you'd just use <code>{slug}</code>.  If it <em>does</em> have a variant, then you'd use <code>{product.slug}</code>.  This should be less awkward in the upcoming Commerce 2 as well.</p>

CC BY-SA 3.0
2017-10-27T14:15:46.293Z
Brad, would there be a way to include an asset field to help in the auto-generation of SKUs? I'd like to have a product image filename be the SKU number. I've tried {productImage.filename} and {product.productImage.filename} and came up empty.
John Johnson
CC BY-SA 3.0
2017-10-27T18:29:11.747Z
Assets are always in array format.  So if you wanted the first Asset, you'd use `{productImage[0].filename}` or `{product.productImage[0].filename}` depending on the variant situation.
Brad Bell
CC BY-SA 3.0
2017-10-27T20:27:31.937Z
Worked wonderfully! Except I didn't realize the file extension was included in filename. Any way to avoid the file extension in the SKU?
John Johnson
CC BY-SA 3.0
2018-02-02T13:55:21.787Z
I can't get this to work for variants... I have the variant SKU set to `{product.slug}` and the automatic SKU format for the product type set to the same... but it isn't being translated into anything on the front end.
James Greig
CC BY-SA 4.0
2020-07-07T08:59:24.363Z
Yes just to add that @BradBell's method does not work for variant products. By setting the value to `{product.title}` the SKU field does not show for any variants, and saving failed because you cant enter a unique SKU. This unfortunately makes commerce near unusable for products with large numbers of variants - you cant expect clients to sit manually entering hundreds of SKU codes that could just auto increment.
Sk446
CC BY-SA 3.0
2017-10-19T18:58:33.677Z
2017-10-19T18:58:33.677Z
Brad Bell
<p>When I activate Auto Generated SKU's in Craft Commerce, I get the "SKU's cannot be left blank" error when I submit a new product. Isn't it the point to leave them blank so they'll auto generate based on other fields?</p>

CC BY-SA 3.0
2017-10-18T22:10:44.060Z
What is your `Automatic SKU Format` setting set to?
Brad Bell
CC BY-SA 3.0
2017-10-19T16:02:43.833Z
I just kept it simple to see how it worked. {product.slug}
John Johnson
<plugin-craftcommerce>
Auto Generated SKU
# Auto Generated SKU

(Asked by: username_0 on 2017-10-18)

When I activate Auto Generated SKU's in Craft Commerce, I get the "SKU's cannot be left blank" error when I submit a new product. Isn't it the point to leave them blank so they'll auto generate based on other fields?

**Tags:** plugin-craftcommerce

> **Comments:**
> **username_1**: What is your `Automatic SKU Format` setting set to?  
> **username_0**: I just kept it simple to see how it worked. {product.slug}  

---

## 1 answers

### Answer by username_1 on 2017-10-19. Score: 1 | Accepted answer

It's a little awkward, but the documentation has a note about this: <https://craftcommerce.com/docs/product-types#automatic-sku-format>

If a product does *not* have a variant, then you'd just use `{slug}`. If it *does* have a variant, then you'd use `{product.slug}`. This should be less awkward in the upcoming Commerce 2 as well.

> **Comments:**
> **username_0**: Brad, would there be a way to include an asset field to help in the auto-generation of SKUs? I'd like to have a product image filename be the SKU number. I've tried {productImage.filename} and {product.productImage.filename} and came up empty.  
> **username_1**: Assets are always in array format.  So if you wanted the first Asset, you'd use `{productImage[0].filename}` or `{product.productImage[0].filename}` depending on the variant situation.  
> **username_0**: Worked wonderfully! Except I didn't realize the file extension was included in filename. Any way to avoid the file extension in the SKU?  
> **username_2**: I can't get this to work for variants... I have the variant SKU set to `{product.slug}` and the automatic SKU format for the product type set to the same... but it isn't being translated into anything on the front end.  
> **username_3**: Yes just to add that @BradBell's method does not work for variant products. By setting the value to `{product.title}` the SKU field does not show for any variants, and saving failed because you cant enter a unique SKU. This unfortunately makes commerce near unusable for products with large numbers of variants - you cant expect clients to sit manually entering hundreds of SKU codes that could just auto increment.  

---

