Shopify-如何将标签的显示顺序与集合中的产品显示顺序相匹配



希望有人能帮上忙,因为我对使用Shopify的液体代码格式还很陌生。

最近,我使用标签在产品过滤器的集合页面上实现了多个下拉排序框。基于此";如何":https://community.shopify.com/c/Shopify-Design/How-to-Multiple-Dropdown-Sorting-Boxes-on-Collection-...

所有这些都按预期工作,但我在订购其中一个过滤器的标签时遇到了问题。我希望标签的显示顺序与集合中产品的当前顺序相匹配(这些产品是从后端手动订购的(-请参阅图像

收集页面和过滤器的屏幕截图

处理此特定下拉列表的代码:

<div class="four-columns">
Package size:
<select class="coll-filter" id="sizeFilter">
<option class="size-all" value="">All</option>
{% for tag in collection.all_tags %}
{% if tag contains 'size-' %}
{% assign tagName = tag | remove: 'size-' %}         
{% if current_tags contains tag %}
<option class="{{tag}}" value="{{ tag | handle }}" selected>{{ tagName }}</option>
{% else %}
<option class="{{tag}}" value="{{ tag | handle }}">{{ tagName }}</option>
{% endif %}                     
{% endif %}
{% endfor %}
</select>
</div>  

我的主要缺点是我不能"硬编码";列表,因为存在具有不同包大小的多个集合。我们非常感谢所有的帮助。谢谢

如果你想基于产品的订单,那么你需要使用产品,而不是抓取所有的标签。

所以它会变成这样:

{% assign all_tags = '' %}
{% for product in collection.products %}
{% for tag in product.tags %}
{% if tag contains 'size-' %}
{% assign all_tags = all_tags | append: tag | append: ',' %}
{% endif %}
{% endfor %}
{% endfor %}
{% assign all_tags_unique = all_tags | split: ',' | uniq %}
<select>
{% for tag in all_tags_unique %}
{% assign tagName = tag | remove: 'size-' %}         
{% if current_tags contains tag %}
<option class="{{tag}}" value="{{ tag | handle }}" selected>{{ tagName }}</option>
{% else %}
<option class="{{tag}}" value="{{ tag | handle }}">{{ tagName }}</option>
{% endif %} 
{% endfor %}
</select>

其中,我们将循环当前产品,并将它们的每个标签推送到可变{% assign all_tags = '' %}

在我们完成产品后,我们将通过除法器对其进行分割,在我们的情况下,除法器是,,并将添加唯一的过滤器,该过滤器将删除阵列中的任何重复(如果产品或重复上有多个大小(。

最后,您将循环使用新创建的标记数组all_tags_unique

仅此而已。

PS:请记住,您有50多种产品,需要增加页码才能全部获得。


请记住,这不是经过测试的代码,因此可能会有一两个错误,但逻辑是存在的。:(

最新更新