Ad

Shopify Sold Out Message

- 1 answer

I'm trying to display a sold out message when we set a product's quantity to 0. This bit of liquid code of kinda worked:

`{% assign variantQuantity = product.variants | map: 'inventory_quantity' | sort %}
{% if variantQuantity[0] < 1 %}
<strong><p style="color: #B21F1F;">This item is currently sold out.</p></strong>
{% else %}
{% endif %}`



The problem is it displays the sold out message when even one size is out of stock and other sizes are not. Is there a way to check and make sure all sizes are sold out?

Ad

Answer

You can just check the product.available attribute:

product.available

Returns true if a product is available for purchase. Returns false if all of the products variants' inventory_quantity values are zero or less, and their inventory_policy is not set to "Allow users to purchase this item, even if it is no longer in stock."

So you can use for example:

{% if product.available == false %}
  This item is currently sold out.
{% endif %}
Ad
source: stackoverflow.com
Ad