问题
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:
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.
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