Shopify:从集合中获取价格最高的商品 [英] Shopify: Getting highest price item from collection
问题描述
有人知道我如何从某个类别中获得价格最高的产品吗?如果通过Liquid进行比较会更好,但是如果使用JavaScript也可以.任何想法都欢迎:P我只需要在某一类别上获得最高价的商品即可.
Does anyone know how I could get the highest price product from a category? It would be better if it was via Liquid, but if its with JavaScript its ok as well. Any ideas are welcome :P I only need to get the highest price item on a category.
我偶然发现了下一个流动代码:
Ive stumbled upon the next liquid code:
{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
<h4>{{ product.title }}</h4>
{% endfor %}
但是我似乎无法通过降价来解决这个问题.有没有办法做到这一点?任何人? :P
But I cant seem to get this working to sort by price-descending. Is there a way to do this? anyone? :P
供以后参考,这是我在其中找到的链接:
for future reference here is the link where I found this:
https://docs.shopify.com/themes/liquid-documentation/filters/array-filters#sort
先谢谢您了:D
干杯!
推荐答案
取决于您将如何使用信息,那么一种实现方法是使用iframe hack:
Depending on how you are going to use the information then one way to do this is to use an 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
的新收集页面,并在其中添加下一个代码
Ive actually found a faster way of getting the highest price. Cause right now, you´re basically loading the collection page two times to find out. All you would have to do is create a new collection page named for example collection.hprice.liquid
and add the next code in it
{% 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')
更改为此
Now on the code you had, simply change the iframe.setAttribute('src')
to this
iframe.setAttribute('src', '?sort_by=price-descending&checkhigh=T&view=hprice');
这将更快,因为它将仅在hprice视图上加载div:D之后,您可以像addEventListener上的else一样删除不必要的代码.
And it will be much faster cause it will load only a div on the hprice view :D After that you can delete needless code like the else on the addEventListener.
这篇关于Shopify:从集合中获取价格最高的商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!