2017-09-14 21 views
1

我們一直在努力解決這個問題數週,仍然沒有找到解決方案。Shopify Liquid Search.Results - 如何按條形碼排序?

我們有一個擁有2000個產品和300個供應商的Shopify商店。每個供應商都有一個我們分配給他們的「排名編號」,並將其輸入到每個產品變體中的「條形碼」字段中。

的目標是,我們要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對我們的搜索結果進行排序,那將是非常令人高興的!!!!!

感謝

+0

我沒有設置任何條碼來測試這個,但你有沒有試過'{%assign foo = search.results | map:'item'| sort:'barcode'%}'? 'map'過濾器會將search.results簡化爲只包含一系列項目,然後可以使用排序過濾器進行排序... –

回答

1

在本機功能you're out of luck條款。

你可以做這樣的事情,雖然通過滾動你自己的。

  1. 手動排序您的收藏由供應商排名
  2. 限制通過創建自己的搜索功能 或
  3. 科瑞自己的搜索功能,但要在當前的集合「過濾器」搜索到的收集結果和單獨離開本機搜索。

創建自己的搜索:

  1. 把你的項目結果的小區中的一個片段。 collection-product.liquid
  2. 將這段代碼包含在 您的collecton液體中。
  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 
     } 
    } 
};