How do I check if a product has stock in any of it's variants? Basically, I'm trying to show "Out of stock" and disable a button for when none of the variants have stock, and unlimited stock is unchecked.

I've tried this, which isn't working:

{% if product.variants.stock &lt;= 0 and product.variants.unlimitedStock == false %}
    {{ "Out of stock"|t }}
{% else %}
    {{ "Add to cart"|t }}
{% endif %}
I managed do this with the following:

{% set variants = craft.commerce.variants({
    product: product,
    hasStock: true
}) %}

{% if variants|length &lt;= 0 %}
  Sold out
{% else %}
  Add to basket
{% endif %}


The hasStock criteria takes unlimitedStock into consideration.

if (isset($criteria-&gt;hasStock) &amp;&amp; $criteria-&gt;hasStock === true)
{
  $hasStockCondition = ['or', '(variants.stock &gt; 0 AND variants.unlimitedStock != 1)', 'variants.unlimitedStock = 1'];
  $query-&gt;andWhere($hasStockCondition);
}