Usually you will only want to retrieve products, giving you access to their variants. Sometimes you might want to query for variants directly.
You can access your site’s variants from your templates via craft.variants
It returns an ElementQuery object.
{% set variant = craft.variants.id(8376).one() %}
{% if variant %}
{{ variant.sku }} - {{ variant.salePrice }}
{% endif %}
craft.variants
supports the following parameters:
The variant's element ID.
The product ID this variant belongs to.
The variant's SKU
Whether the variant is the product's default variant
Accepts: boolean (true
or false
)
The stock amount
Accepts: integer
Whether the variant has unlimited stock or stock greater than zero.
Accepts: boolean (true
or false
)
Returns variants that have at least one sale available to the current user.
Accepts: true
or false
For example:
{% set products = craft.products({
hasSales: true
}) %}