How Do I Search Everything But Hide Sold Out – Shopify on-site Search Results
Shopify is an amazing platform, however, they could seriously benefit from an 0n-site search engine overhaul. If you have ever tried to edit your search.liquid file, then you have probably realized what you can do is very limited and you could spend hours trying to make the smallest of changes to hide or sort your products, pages, or articles. Today I will be sharing with you the quick and easy way to hide sold out products from your “Search Everything” search query by using a tag to exclude sold out items (or any other tag) from your search results. This allows you to keep the products available on your site for SEO purposes without hiding the product pages from other parts of the site or search engines such as Google.
Back-Up Your Theme
As always, you want to make a back-up of your theme. You can either export it to a zip file or “Duplicate”. By selecting duplicate, a copy will appear below your published theme in the unpublished theme area. Once you are happy with the changes you make to your unpublished theme, you then press the publish button to replace your live theme.
Open Your Search File
Depending on your theme, this could be in different places. Most common is search.liquid under templates but if your theme uses snippets or sections, your search.liquid may point to one of those locations.
Add 2 Lines of Code
In this solution, we are going to hide products based on tag. For the site I am working on, the tag is sold.
Find this line of code:
{% for item in search.results %}
Directly below it add:
{% unless item.tags contains 'sold' %}
The second line of code that you need to add is you need to close your unless statement.
If you are new to editing code in Shopify, simply press save and an error will appear at the top. Click the Line number to shoot down to the line in question. Depending on your file you may see an endif or endfor on that line. DO NOT REMOVE IT (as the error states you should). Press enter to create a new line above it and enter:
{% endunless %}
SAVE.
“Preview” and test your search.