Show/Hide Products With Zero Inventory
This wiki will go over how to add a show/hide product functionality for products that have 0 inventory left, and how to add it to the admin in the backend.
First, open your settings_schema.json, and create a new setting. Create something similiar to the code below.
{
"name": "Hide If Inventory is 0",
"settings": [
{
"type": "select",
"id": "hide_if_inventory_zero",
"label": "Enable/Disable Hide",
"options": [
{
"value": "enabled",
"label": "Enable"
},
{
"value": "disabled",
"label": "Disable"
}
]
}
]
},
If the setting is enabled, products with no inventory will be hidden. Otherwise the products will be displayed. Make sure the id is something unique to be called upon.
Next, open up your product-loop.liquid, and add in this logic at the top.
{% if settings.hide_if_inventory_zero == "enabled" %}
{% if product-loop.available == true %}
At the bottom of product-loop.liquid, wrap up the file with this.
{% endif %}
{% endif %}
Below the two endifs at the bottom page, add the beginning of last part of the logic below.
{% if settings.hide_if_inventory_zero == "disabled" %}
Copy and paste the normal content of product-loop.liquid underneath, and wrap up the logic with a closing endif.
{% endif %}
Your product-loop.liquid should now look something like this. And you're done!
{% if settings.hide_if_inventory_zero == "enabled" %}
{% if product-loop.available == true %}
{% endif %} {% endif %} {% if settings.hide_if_inventory_zero == "disabled" %}
{% endif %}