Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I am trying to hide a few products from my search results and need help figuring out where to put the code. Nothing seems to be working for me.
There is a metafield solution but it doesnt work for me as I have an error.
Here is my code for my empire theme. (search.header.liquid)
{% layout none %}
{% comment %}
Use 'search-sanitizer' to remove added `*` added by HeaderSearch
- Adding `*` after a word helps Shopify increase amount of results
{% endcomment %}
{% capture sanitized_terms %}
{%
render 'search-sanitizer',
search_terms: search.terms
%}
{% endcapture %}
{% assign sanitized_terms = sanitized_terms | strip %}
{%- paginate search.results by 50 -%}
{%- capture product_results -%}
{% assign product_count = 0 %}
{%- for item in search.results -%}
{% if product_count >= 5 %}
{% break %}
{% endif %}
{%- if item.object_type == 'product' -%}
{% comment %}
Elevate boolean logic to data source as much as possible
{% endcomment %}
{% assign on_sale = false %}
{% if item.compare_at_price > item.price %}
{% assign on_sale = true %}
{% endif %}
{% assign consistent_saved = false %}
{% capture consistent_savings %}
{%
render 'consistent-savings',
variants: product.variants,
on_sale: on_sale
%}
{% endcapture %}
{% if consistent_savings contains 'true' %}
{% assign consistent_saved = true %}
{% endif %}
{%- assign swatch_count = nil -%}
{%- if settings.swatches_enable -%}
{%- assign swatch_trigger = settings.swatches_swatch_trigger | strip | downcase -%}
{%- for option in item.options_with_values -%}
{%- assign option_name = option.name | downcase -%}
{%- if option_name == swatch_trigger -%}
{%- assign swatch_count = 'product.color_swatches.swatches_count' | t: count: option.values.size -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- capture product_image -%}
{% if item.featured_media %}
{% assign image_alt = item.title | escape %}
{% if item.featured_media.alt != blank %}
{% assign image_alt = item.featured_media.alt | escape %}
{% endif %}
{%
render 'rimg',
img: item.featured_media.preview_image,
alt: image_alt,
size: '100x100',
%}
{% else %}
{{ 'image' | placeholder_svg_tag: 'placeholder--image' }}
{% endif %}
{%- endcapture -%}
{%- assign stock_level = nil -%}
{% if settings.inventory_status %}
{%- capture stock_level -%}
{%
render 'product-stock-level',
inventory_display: settings.inventory_display,
inventory_transfer: settings.inventory_transfer_notice,
variant: item.selected_or_first_available_variant,
verbose: false,
%}
{%- endcapture -%}
{% endif %}
,
{
"title": {{ item.title | highlight: sanitized_terms | json }},
"id": {{ item.id | json }},
"handle": {{ item.handle | json }},
"on_sale": {{ on_sale | json }},
"consistent_saved": {{ consistent_saved | json }},
"price": {
"price": {{ item.price | json }},
"price_min": {{ item.price_min | json }},
"price_max": {{ item.price_max | json }},
"compare_at_price": {{ item.compare_at_price | json }},
"compare_at_price_min": {{ item.compare_at_price_min | json }},
"compare_at_price_max": {{ item.compare_at_price_max | json }},
"compare_at_price_varies": {{ item.compare_at_price_varies | json }}
},
"image": {{ product_image | strip_newlines | json }},
"url": {{ item.url | json }},
"stock_level": {{ stock_level | json }},
"swatch_count": {{ swatch_count | json }}
}
{% assign product_count = product_count | plus: 1 %}
{%- endif -%}
{%- endfor -%}
{%- endcapture -%}
{%- capture content_results -%}
{%- for item in search.results -%}
{% if item.object_type == 'article' or item.object_type == 'page' %}
,
{
"title": {{ item.title | json }},
"url": {{ item.url | json }}
}
{%- endif -%}
{%- endfor -%}
{%- endcapture -%}
{%- endpaginate -%}
{
"content": [{{ content_results | strip_newlines | remove_first: ',' }}],
"products": [{{ product_results | strip_newlines | remove_first: ',' }}],
"terms": {{ search.terms | json }},
"sanitizedTerms": {{ sanitized_terms | json }}
}
(This is for the search.header.liquid)
Hey, @tritonebear
I'm confident one of our Shopify experts would be able to accomplish this at a reasonable price. Sharing this as an option in case you are unable to find a coding solution in this thread.
If there is anything else I can help you with, please let me know.
Dirk | Social Care @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hi @tritonebear
You have to figure out how to call those products that you want to be hidden. You can then add an unless statement for the forloop. You do need to hire a developer if you are not familiar with the code
{%- unless item.id == 'id' -%}
Your product viewer here...
{% endunless %}
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024