Shopify:从 collection 获取最高价商品

Shopify: Getting highest price item from collection

有谁知道我怎样才能从一个类别中获得价格最高的产品?如果它通过 Liquid 会更好,但如果它带有 JavaScript 也可以。欢迎任何想法 :P 我只需要获得某个类别中价格最高的商品。

我偶然发现了下一个液体代码:

{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
    <h4>{{ product.title }}</h4>
{% endfor %}

但我似乎无法按 price-descending 排序。有没有办法做到这一点?任何人? :P

为了将来参考这里是 link 我发现这个的地方:

https://docs.shopify.com/themes/liquid-documentation/filters/array-filters#sort

提前致谢:D

干杯!

将您获得的一些资源放在一起:

  {% assign products = collections[settings.frontpage_collection].products | sort: 'price' %}   

<h2>{{ products[-1].title }}</h2> <!-- last sorted product, -2 is second last etc -->

  {% for product in products reversed %} <!-- list in reverse sorted order -->

我认为没有办法直接从您的 liquid 文件中执行此操作。有(据我所知,不包括任何 Javascript 骇客)两种方法让它工作,但这取决于你的 collection 是如何被拉到页面上的。

如果 collection 在常规 collection 页面上,即 my-store.myshopify.com/collections/my-collection,您可以将 ?sort_by=price-descending 查询添加到URL 的末尾,它将提取您的项目,按价格排序,并且还可以使用分页。 This page 尤其是帮我解决了这个问题。

如果您从 non-collection 页面拉取 collection,并且您只想按 price-descending 对其进行排序,您实际上可以直接从 collection 对其进行排序] 的页面在管理仪表板中。只需 select 您要排序的 collection 并查找 "Sort" 下拉列表。

请记住,这会影响此 collection 在所有页面上的排序。因此,如果您想在一个页面上以这种方式排序,但在另一个 non-collection 页面上以另一种方式排序,那么这可能不是正确的解决方案。

如果以上选项都不适合您,那么我认为唯一的方法是使用 Javascript 提取所有项目,按价格排序,然后 select应该显示在当前页面上,具体取决于您的分页设置方式。

祝你好运!

根据您打算如何使用这些信息,一种方法是使用 iframe hack:

{ % assign products = collection.products | sort: 'price' % } 
<div id="highPriceItem" style="display:none;">{{ products[-1].title }}</div> 

<script> function receiveHighPriceItem(elem) {
    //extract info here and do whatever with it.
    alert('High Item: ' + elem.firstChild.nodeValue);
}
window.addEventListener('load', function() {
    if (!window.parent || window.parent === window) { // check so no infinite iframes
        // jQuery free example
        var iframe = document.createElement('IFRAME');
        iframe.setAttribute('style', 'visibility:hidden;height:0; width:100%;');
        iframe.setAttribute('src', '?sort_by=price-descending&checkhigh=T');
        document.getElementsByTagName('BODY')[0].appendChild(iframe);
    } else if (window.parent && window.parent !== window && window.parent.receiveHighPriceItem) {
        window.parent.receiveHighPriceItem(document.getElementById('highPriceItem'));
    }
}); 
</script>

/** 编辑 **/

我实际上找到了获得最高价格的更快方法。因为现在,您基本上需要加载两次产品系列页面才能找到答案。您所要做的就是创建一个新的集合页面,例如 collection.hprice.liquid 并在其中添加下一个代码

{% layout none %}
<script type="text/javascript">
  window.addEventListener('load', function() {
    if (window.parent && window.parent !== window && window.parent.receiveHighPriceItem) {
      window.parent.receiveHighPriceItem(document.getElementById('highPriceItem'));
    }
  });
</script>

<!-- Thanks to bknights for the code :D -->
{% assign products = collection.products | sort: 'price' %} 
<div id="highPriceItem" style="display:block;">{{ products[-1].price_max | divided_by: 100 }}</div> 

现在在您的代码上,只需将 iframe.setAttribute('src') 更改为

iframe.setAttribute('src', '?sort_by=price-descending&checkhigh=T&view=hprice');

而且速度会快得多,因为它只会在 hprice 视图上加载 div :D 之后,您可以像 addEventListener 上的其他代码一样删除不必要的代码。