【问题标题】:Shopify Liquid Search.Results - How to Sort by Barcode?Shopify Liquid Search.Results - 如何按条形码排序?
【发布时间】:2017-09-14 19:53:40
【问题描述】:

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

我们的 Shopify 商店拥有 2000 种产品和 300 家供应商。每个供应商都有一个我们分配给他们的“排名编号”,并输入到每个产品变体的“条形码”字段中。

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

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

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

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

我们已尝试将“排序”与以下所有参数一起使用,但它们都不起作用:

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

等,但它们都不起作用。 如果任何人都可以为我们指出正确的方向,以使用哪个参数/语法来按 variant.barcode 对我们的搜索结果进行排序,那将非常感谢!!!!!!

谢谢

【问题讨论】:

  • 我没有设置任何条形码来测试这个,但你试过{% assign foo = search.results | map: 'item' | sort: 'barcode' %} 吗? map 过滤器会将 search.results 减少为一个项目数组,然后应该可以使用排序过滤器对其进行排序...

标签: sorting shopify liquid


【解决方案1】:

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

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

  1. 按供应商等级手动排序您的收藏
  2. 通过创建自己的搜索功能将搜索限制为集合结果 或
  3. 创建您自己的搜索功能,但将其设置为您当前集合的“过滤器”,不理会本机搜索。

创建您自己的搜索:

  1. 将您的项目结果单元格放入一个 sn-p。 collection-product.liquid
  2. 将 sn-p 包含在 你的收集液。
  3. 制作新的收藏模板 (collection.search-results.liquid)

collection.search-results.liquid 如下所示。您的分页大小应与主集合的分页大小相匹配。

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 2019-02-18
    • 2022-11-25
    • 1970-01-01
    • 2014-08-19
    • 2014-10-02
    • 2018-04-18
    • 2013-05-21
    相关资源
    最近更新 更多