代码之家  ›  专栏  ›  技术社区  ›  qwerty123

Shopify Liquid Search。结果-如何按条形码排序?

  •  2
  • qwerty123  · 技术社区  · 8 年前

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

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

    然而,当我们尝试使用此语法按“条形码”排序时,它不会返回任何结果

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

    我们尝试将“sort”与以下所有参数一起使用,但均无效:

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

    等等,但都不起作用。 如果有人能告诉我们使用哪个参数/语法按变量对搜索结果进行排序的正确方向。条形码,非常感谢!!!!!

    谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   bknights    8 年前

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

    不过,你可以通过自己滚动来做类似的事情。

    1. 按供应商排名手动排序您的收藏
    2. 创建自己的搜索功能,但将其作为当前收藏的“过滤器”,不要使用本机搜索。

    创建自己的搜索:

    1. 将项目结果单元格放入代码段中。收集产品。液体
    2. 你的收藏液。
    3. 创建新的集合模板

    收集搜索结果。液体看起来像下面这样。您的分页大小应与主收藏的分页大小相匹配。

    {% layout none %}
    {% paginate collection.products by 50 %}
    <div class="search-results">
    {% for product in collection.products %}
      {% include 'collection-product' %}
    {% endfor %}
    </div>
    {% endpaginate %}
    

    至于为什么不对结果进行排序。您可以使用这个概念来实现这一点,但它每次返回一个页面的结果。为了自定义搜索所有结果,您必须累积结果,并在搜索完成后对其进行排序。搜索时间在很大程度上取决于收集的大小和用户的网络速度。

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