Shopify Liquid Search.Results - 如何按条码排序?

Shopify Liquid Search.Results - How to Sort by Barcode?

我们已经为这个问题苦苦挣扎了数周,但仍未找到解决方案。

我们有一个 Shopify 商店,有 2000 种产品和 300 个供应商。每个供应商都有一个 "ranking number",我们已将其分配给他们并输入到每个产品变体的 "barcode" 字段中。

我们的目标是希望 Shopify 能够使用 liquid "sort" 标签按条形码对搜索结果进行排序,如下所示:

{% assign foo = search.results | sort:'title' %}

但是,当我们尝试使用此语法按 "barcode" 排序时,return 没有任何结果

{% assign foo = search.results | sort:'barcode' %}

我们已尝试将 'sort' 与以下所有参数一起使用,但其中 none 有效:

{% assign foo = search.results | sort:'product.barcode' %}
{% assign foo = search.results | sort:'item.barcode' %}
{% assign foo = search.results | sort:'item.variants.first.barcode' %}
{% assign foo = search.results | sort:'items.variants.variant.barcode' %}

等,但其中 none 有效。 如果任何人都可以为我们指出 argument/syntax 用于按 variant.barcode 对我们的搜索结果进行排序的正确方向,我们将不胜感激!!!!

谢谢

就本机功能而言you're out of luck

你可以通过滚动自己来做这样的事情。

  1. 按供应商等级
  2. 手动排序您的 collection
  3. 通过创建您自己的搜索功能将您的搜索限制为 collection 个结果 或
  4. 创建您自己的搜索功能,但将其设为您当前 collection 的 "filter" 并单独使用本机搜索。

创建您自己的搜索:

  1. 将您的项目结果单元格放在代码段中。 collection-product.液体
  2. 将该代码段包含在 你的收集液。
  3. 制作一个新的collection模板 (collection.search-results.液体)

collection.search-results.liquid 看起来像下面这样。您的分页大小应与主 collection.

的分页大小相匹配
{% layout none %}
{% paginate collection.products by 50 %}
<div class="search-results">
{% for product in collection.products %}
  {% include 'collection-product' %}
{% endfor %}
</div>
{% endpaginate %}

现在您的搜索变成了一个带有一些 javascript 的输入框。有趣的部分。下面的脚本是从一个工作站点中提取的。我对它进行了一些修整,希望能使它的骨架更清晰,但它可能不会像现在这样 运行。我删除了对添加搜索的引用,因为出于您的目的,您需要本机自定义搜索。

至于为什么不直接对结果进行排序。您可以使用此概念来执行此操作,但它 returns 一次生成一个页面。为了自定义搜索所有结果,您必须累积结果并在搜索完成时对它们进行排序。搜索时间在很大程度上取决于 collection 的大小和用户的网络速度。

var defaultView = $(".filters-off"); // add this class to your main collection wrapper
var facetView = $("#filter_target"); // <div id="filter_target"></div> below your main collection wrapper

var currentRequest = 0;
function filterCollection(term){
    //[[t1, t2], [t3,t4]]
     // console.log('applying '+ JSON.stringify(facets));

    var anyResults = false;
    var resultsPage=0;
    var PAGE_SIZE = 50;
    var productsFound = 0;
    var collectionPath = location.pathname;

    console.log('get from '+ collectionPath);
    currentRequest = new Date().getTime();

    var viewLink = collectionPath+'?view=search-results'; // same as your layout none template

    function applyFilter(myRequest){
        resultsPage++;
        if(resultsPage > 20) return false; // arbitrary abort for too many results
        if(resultsPage > 1) viewLink+='&page='+resultsPage;
        return $.get(viewLink).then(function(page){
            if(currentRequest != myRequest){
                console.log('mid abort');
                return false;
            }
            var pageProducts = $("div[data-tags]", page); //some markup you added to collection-product snippet to help plucking the product elements
            if(!pageProducts.length) return false;

            console.log('found: '+ pageProducts.length);

            var filteredProducts = pageProducts.filter(function(){
                if($(this).text().indexOf(term) != -1) return true; // search the returned text

                if($(this).attr('data-keywords').indexOf(term) != -1) return true;
                return false;
            });
            if(filteredProducts.length){
                if(!anyResults){
                    anyResults = true;
                    toggleView(true);

                }
                filterView.append(filteredProducts);
                productsFound+= filteredProducts.length;
            }
            return (pageProducts.length == PAGE_SIZE && currentRequest == myRequest) ? applyFilter(myRequest) : false;
        }).then(function(proceed){
            if(currentRequest == myRequest){

                if(!anyResults){
                    toggleView(false, true);
                }
            }
        });
    }
    applyFilter(currentRequest);
}


function toggleView (showFacets, showNoGo){
    facetView.empty();
    $(".filter-progress").empty();
    if(showFacets) {
        defaultView.add('.pagination').hide();

    }else {

        if(!showNoGo){
            defaultView.add('.pagination').show();
        }else {
            $(".no-facets").clone(true).appendTo(facetView).show(); // .no-facets is normally hidden chunk that allows for easy internationaliztion of "No results found" type messages
        }
    }
};