Re: Display as out-of-stock when inventory >0

Display as out-of-stock when inventory >0

hill_jonathan
Tourist
8 0 2

Hello,

 

We would like product variants to display as "Out of Stock" on our website when their inventory level drops below a certain number given by a variant metafield. This will give us a little buffer for fast-selling SKUs which might sell out before we can set aside the item for shipping/in-store pickup. We do not want to actually change the inventory level to 0 (as discussed in this post: https://community.shopify.com/c/shopify-discussions/can-you-automate-inventory-to-mark-items-as-out-...) or to hide/unpublish the product from our website; we just want it to look like the product is out-of-stock. Is this possible? I understand we will probably have to enter the code in our theme.

 

Thanks to everyone for the assistance.

Replies 2 (2)

JoesIdeas
Shopify Partner
2500 229 673

I think you'd want to check the inventory of the variant, then if < a certain number, hide the add to cart button.

 

This will help to try variables:https://shopify.dev/docs/api/liquid/objects

 

I would try something like this:

{% if variant.inventory_quantity < 10 %}
<p class="out-of-stock">Out of stock</p>
{% else %}
Regular add to cart button here
{% endif %}

 

Try putting that where your add to cart button is (maybe in a snippet like product-form.liquid, you'll have to read the code in your theme files to see where).

 

If that doesn't work, then experiment with other relevant product and variant variables on that page, for example, if you have multiple variants for the product, you might have to cycle through them with a {% for %} loop. You can find all that in the Liquid documentation.

 

Once you get the code working, then create a CSS class out-of-stock to style it.

 

Duplicate your live theme first so you can work on a backup theme without affecting the live site.

• Creator of Order Automator [auto tag, fulfill, connect FBA, daily jobs]
• Co-Creator of Product Automator [suite of features for products / collections]
• Shopify developer for 10+ years, store owner for 7 years
• Blog: Shopify Tips, Guides, and Automation Tactics
hill_jonathan
Tourist
8 0 2

Thank you Joe -- not quite as easy as I was hoping, but makes sense!