Shopify JSON array returning results but some empty results

北战南征 提交于 2020-01-05 05:01:41

问题


I am wanting to pull back only results that are available but it is returning in the json response null,null,null,result, result for example. Here is the code:

{% capture results %}
  {% for item in search.results %}
    {% assign product = item %}
    {% if product.available == true %}
      { 
        "title"    : {{ product.title | json }},
        "url"      : {{ product.url | within: product.collections.last | json }},
        "thumbnail": {{ product.featured_image.src | product_img_url: 'thumb' | json }},
        "available": {{ product.available | json }}
      }
    {% endif %}
    {% unless forloop.last %},{% endunless %}
  {% endfor %}
{% endcapture %}

Anyone know what i would need to do to not return the null results but only the available true ones and exclude the available(false)

It returns 9 results, 7 of which are not available and would show 2 but need it to return 9 available not 2 out of the 9 if you know what i mean.


回答1:


There are two things you can do here:

  1. You can create a search app outside of Shopify. This app will fetch all the products, check each product for availability condition and return the search results in arrays of 10, 20, 30 etc products, which you can store locally the session if you intend to do pagination.

  2. For each product that is available on your store, add a tag istock (In Stock) and for those that are not available have a tag ostock. While searching, just append +istock to the search query. It'll always return only the products matching the search term and with the tag "istock".



来源:https://stackoverflow.com/questions/39594034/shopify-json-array-returning-results-but-some-empty-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!