How can I write an if else statement in Twig based on commerce stock quantity? Building a D8 Commerce site and trying to hide the price if the product is out of stock.
I can access the stock value with this:
{{ product.variation_field_stock_level.0 }}

I've tried this:
{% if product.variation_field_stock_level.0 &gt; 0 %}
 {{ product.variation_price }}
{% else %}
 &lt;p&gt;Sold&lt;/p&gt;
{% endif %}

...but it prints the price regardless of whether the stock value is 0 or 1 (or more).
I've also tried:
{% if product.variation_field_stock_level.0 == 0 %}
  &lt;p&gt;Sold&lt;/p&gt;
{% else %}
  {{ product.variation_price }}
{% endif %}

Regardless of what I try it just will not work.
This is very close - but it seems you are missing the actual stock level value from the object. Start by doing something like:
{{product.variation_field_stock_level.0|json_encode}}

Run it through a json cleaner to make it readable, like: JSON Cleaner
You will see this:
{&quot;#type&quot;:&quot;html_tag&quot;,&quot;#tag&quot;:&quot;p&quot;,&quot;#value&quot;:0}

Now the conditional will be:
{% if product.variation_field_stock_level.0['#value'] &gt; 0 %}
    {{ product.variation_price }}
{% else %}
    &lt;p&gt;Sold&lt;/p&gt;
{% endif %}