Shopify: Dynamic link to hide out of stock

423 views Asked by At

I have the below code in my shopify site, currently all it does is display a link to either 'in stock' or 'Show All'. I would like to make the link more dynamic so if someone has filtered by a certain product and they click on the "in stock" link it would show only in stock for that product. eg. if they were at /collections/all/Product1 after clicking the link it should go to /collections/in-stock/Product1

My Current code:

<div class="filter-stock">
{% if page_title contains "Products" %}
            <a href="/collections/in-stock"><b>Hide 'Sold Out' items</b></a>
{% endif %} 
{% if page_title contains "IN STOCK" %}
            <a href="/collections/all"><b>Show All Products</b></a>
{% endif %}

New Code which seems to work:

<div class="filter-stock">
    {% if current_tags %}
        {% for tag in current_tags %}
            {% if collection == blank or collection.handle != 'in-stock' %}
                <a href="/collections/in-stock/{{ tag | handleize }}"><b>Hide 'Sold Out' items</b></a>
            {% endif %} 
            {% if collection and collection.handle == 'in-stock' %}
                <a href="/collections/all/{{ tag | handleize }}"><b>Show All Products</b></a>
            {% endif %}
        {% endfor %}
    {% else %}
        {% if collection == blank or collection.handle != 'in-stock' %}
            <a href="/collections/in-stock"><b>Hide 'Sold Out' items</b></a>
        {% endif %} 
        {% if collection and collection.handle == 'in-stock' %}
            <a href="/collections/all"><b>Show All Products</b></a>
        {% endif %}
    {% endif %}
</div>
1

There are 1 answers

1
koosa On

I would have it set up something like this:

<div class="filter-stock">
    {% if collection == blank or collection.handle != 'in-stock' %}
        <a href="/collections/in-stock{% if product %}/{{ product.handle }}{% endif %}"><b>Hide 'Sold Out' items</b></a>
    {% endif %} 

    {% if collection and collection.handle == 'in-stock' %}
        <a href="/collections/all{% if product %}/{{ product.handle }}{% endif %}"><b>Show All Products</b></a>
    {% endif %}
</div>