I need to get a product variant as an object in Twig, as well as get its parents details (the product fields, not just the variant fields). My first guess starts with this:

{% set variant = craft.commerce.products.sku('my-sku-number') %}

But that doesn't work. I've also tried adding a the .first() limiter to the end. What am I missing?


Thoughts... should I be trying a search/find?


Untested, but something like this should work:

{% set products = craft.commerce.products({
  hasVariant: {sku: 'my-sku-number'}
}).first() %}

See: https://craftcommerce.com/docs/craft-commerce-products#hasvariant

If you only want to search the default variant of the product:

{% set products = craft.commerce.products({defaultSku : 'my-sku-number'}).first() %}

See: https://craftcommerce.com/docs/craft-commerce-products#defaultsku

  • Thanks Luke, that works. Turns out the client entered some skus to search with a related product, before adding those specific products and skus. Nothing like deadlines between developers and marketers!
    – jrothafer
    Nov 17 '17 at 5:06

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.