d
Topic
Posts:
7
January 28, 2016

Excluding Sold Out products from search, but including pages This post is outdated

Hello all,

I have been trying to implement this on a client's store, and I pretty much have it, but need a little help. I want to exclude Sold Out products from the search results. I added the line of code

  {% if item.available %}   

To my search.liquid template, and that seems to have filtered the search results to only show available products (yay!).

BUT, it seems now that I'm ONLY getting products in the results, and I also want to return pages in the search results as well. How can I filter the results to show pages AND available products?

Thanks in advance!

i
Replies
Dave Hughes Member
Posts:
59
Last edited January 29, 2016

idk without trying with the code infront of me might require something a little hacky! but maybe try this.

{% if item.available %}
search code here
{% endif %}

{% unless item.product %}
duplicate the search code here also
{% endif %}

If not you can also try something with the search box rather than the search.liquid. you can use hidden fields and some js stuff. Which is what you want to avoid.

Posts:
7
January 29, 2016

Thanks for the reply! I think I'm doing something wrong with my code- I don't really know php, but I can usually piece it together (dangerous, I know:) Anyway, this is the code I've got so far; when I tried to add yours in, I'm getting an error message but I'm sure it's with my syntax...

 

  <!-- Begin results -->
  {% for item in search.results %}
 
  {% if item.available %}   
 
  <div class="row results">
    <div class="span1">
      <div class="thumbnail">
        <a href="{{ item.url }}">{{ item.featured_image | product_img_url: 'small' | img_tag }}</a>
      </div>
    </div>
    <div class="span11">
      <div class="search-result">
        <h3>{{ item.title | highlight: search.terms | link_to: item.url }}</h3>
        {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}
      </div>
    </div>
    <hr class="divider" />
  </div>
    {% else %}
{% endif %}
      {% endfor %}
  <!-- End results -->

Dave Hughes Member
Posts:
59
Last edited January 29, 2016

<!-- Begin results -->
  {% for item in search.results %}
 
  {% if item.available %}   
 
  <div class="row results">
    <div class="span1">
      <div class="thumbnail">
        <a href="{{ item.url }}">{{ item.featured_image | product_img_url: 'small' | img_tag }}</a>
      </div>
    </div>
    <div class="span11">
      <div class="search-result">
        <h3>{{ item.title | highlight: search.terms | link_to: item.url }}</h3>
        {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}
      </div>
    </div>
    <hr class="divider" />
  </div>
{% endif %}

  {% unless item.product %}   
 
  <div class="row results">
    <div class="span1">
      <div class="thumbnail">
        <a href="{{ item.url }}">{{ item.featured_image | product_img_url: 'small' | img_tag }}</a>
      </div>
    </div>
    <div class="span11">
      <div class="search-result">
        <h3>{{ item.title | highlight: search.terms | link_to: item.url }}</h3>
        {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}
      </div>
    </div>
    <hr class="divider" />
  </div>
{% endunless %}
      {% endfor %}
  <!-- End results -->

Well you don't need the else if you aren't putting anything in it btw. Also im not so sure my item.product is legit I didn't check but if it doesn't work google how to only show products in the search results and revers it using unless or something.

 

If this breaks your paging then it will be a tough one to do from the search box end rather than this search page end.

Posts:
7
Last edited January 29, 2016

Hmmm, thanks! I'll keep plugging away at it- that is including all the available items twice, and everything else once (including pages and sold items). But I think I may be able to figure it out from here. Thanks!

Dave Hughes Member
Posts:
59
Last edited January 29, 2016

That means my item.product was a load of rubbish find the correct term for it :p

Unfortunately anything with the search on shopify is awkward and always feels hacky even when it works.

Posts:
7
January 29, 2016

Thanks for your time! :)

Posts:
7
January 29, 2016

AHA!

I figured it out. First, I found this article here, which was moderately helpful: https://docs.shopify.com/themes/liquid-documentation/objects/search

This is the code I used:

<!-- Begin results -->
  {% for item in search.results %}
 
  {% if item.available %}   
 
  <div class="row results">
    <div class="span1">
      <div class="thumbnail">
        <a href="{{ item.url }}">{{ item.featured_image | product_img_url: 'small' | img_tag }}</a>
      </div>
    </div>
    <div class="span11">
      <div class="search-result">
        <h3>{{ item.title | highlight: search.terms | link_to: item.url }}</h3>
        {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}
      </div>
    </div>
    <hr class="divider" />
  </div>
{% endif %}
 
    {% if item.object_type == 'page' %}   
 
  <div class="row results">
    <div class="span1">
      <div class="thumbnail">
        <a href="{{ item.url }}">{{ item.featured_image | product_img_url: 'small' | img_tag }}</a>
      </div>
    </div>
    <div class="span11">
      <div class="search-result">
        <h3>{{ item.title | highlight: search.terms | link_to: item.url }}</h3>
        {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}
      </div>
    </div>
    <hr class="divider" />
  </div>
{% endif %}
 
   {% if item.object_type == 'article' %}   
 
  <div class="row results">
    <div class="span1">
      <div class="thumbnail">
        <a href="{{ item.url }}">{{ item.featured_image | product_img_url: 'small' | img_tag }}</a>
      </div>
    </div>
    <div class="span11">
      <div class="search-result">
        <h3>{{ item.title | highlight: search.terms | link_to: item.url }}</h3>
        {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}
      </div>
    </div>
    <hr class="divider" />
  </div>
{% endif %}

 
      {% endfor %}
  <!-- End results -->

That gave me search results of pages, articles, and available products, and excludes sold products. Woohoo!!

Dave Hughes Member
Posts:
59
Last edited January 29, 2016

nice :) sorry it was my rubbish knowledge of liquid.

You might also want to move the divs so that it all displays within one row or maybe you want to add titles to clarify the different results or even add something cool like putting them inside tabs you can toggle.

Posts:
7
January 29, 2016

Absolutely! And no worries- I appreciate the help. You definitely pointed me in the right direction!