【问题标题】:Shopify: Dynamic link to hide out of stockShopify:隐藏缺货的动态链接
【发布时间】:2024-01-04 08:25:01
【问题描述】:

我的 shopify 网站中有以下代码,目前它所做的只是显示“有货”或“全部显示”的链接。我想让链接更加动态,所以如果有人过滤了某个产品并且他们点击了“有货”链接,它将只显示该产品的有货。 例如。如果他们在点击链接后位于 /collections/all/Product1,它应该转到 /collections/in-stock/Product1

我当前的代码:

<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 %}

似乎有效的新代码:

<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>

【问题讨论】:

    标签: shopify liquid shopify-template


    【解决方案1】:

    我会设置这样的:

    <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>
    

    【讨论】:

    • 谢谢,我已经使用您发布的内容更新了上面的代码,这似乎工作得很好,任何关于提高效率的指针将不胜感激。