Shopify themes, liquid, logos, and UX
Hi Everyone,
I'm trying to make the default Shopify search field include Metafield data in search results. I found a post with some code that is supposed to work.
This is the post with the code: https://community.shopify.com/c/shopify-design/make-metafield-vaules-come-up-in-the-search-bar/m-p/1...
{% for product in collections.all.products %}
{% assign metafields = product.metafields %}
{% for metafield in metafields %}
{% if metafield.value contains search %}
<a href="{{ product.url }}">{{ product.title }}</a>
{% endif %}
{% endfor %}
{% endfor %}
I'm a novice when it comes to coding (I can understand it when I read it but that's about it) and my question is: Where do I actually insert the code?
There are so many different sections and theme files that I just can't figure out where it would go to effect the results obtained from the header search field. Any help would be greatly appreciated.
Thanks
Hi @SoCalMacGuy ,
If you use Dawn theme. May I suggest to update code these steps:
1. Go to Store Online-> theme -> edit code
2. Sections/main-search.liquid
3. Follow the screenshot to add code below
{% for product in collections.all.products %}
{% assign metafields = product.metafields %}
{% for metafield in metafields %}
{% if metafield.value contains search.terms %}
<li class="grid__item"><a href="{{ product.url }}">{{ product.title }}</a></li>
{% endif %}
{% endfor %}
{% endfor %}
4. Sections/predictive-search.liquid
5. Follow the screenshot to add code below
{% for product in collections.all.products %}
{% assign metafields = product.metafields %}
{% for metafield in metafields %}
{% if metafield.value contains predictive_search.terms %}
<li><a href="{{ product.url }}">{{ product.title }}</a></li>
{% endif %}
{% endfor %}
{% endfor %}
Thank you for the reply. I wish we were using the Dawn theme as those two section names would make it much easier. Unfortunately they decided on the Momentum theme v3.1.1 and for now that's what I have to work with. They seem to have their own naming conventions for a lot of basic things.
Hey!
Does this only work with your specific theme? It did not work for me. I am not really good with coding, so I do not understand whether I need to adapt the code snippet to include the name of the exact metafield and if so, how?
Any help is appreciated.
Hi @breaddddd ,
It is only work for free themes. If you use a fee theme. We need check structure that theme to insert code.
Hey,
thank you for your reply. I have the BeYours Theme, by RoarTheme. I have the section called "predictive-search.liquid" too. Even though I entered the code snippet there, it did not work for me. Or do I need to publish that code to see it in action? So far I am adding a code to a duplicated theme in my library and then just check the preview.
Hi ,
You try to code below:
{% for product in collections.all.products %}
{% assign metafields = product.metafields.custom %}
{% for metafield in metafields %}
{% assign value = metafield[1] %}
{% if value contains predictive_search.terms %}
<li><a href="{{ product.url }}">{{ product.title }}</a> </li>
{% endif %}
{% endfor %}
{% endfor %}
Hey.
thanks for your help.
I added the following code, as you requested:
{% for product in collections.all.products %}
{% assign metafields = product.metafields.mm-google-shopping.custom_label_0 %}
{% for metafield in metafields %}
{% assign value = metafield[1] %}
{% if metafield.value contains search.terms %}
<li class="grid__item"><a href="{{ product.url }}">{{ product.title }}</a></li>
{% endif %}
{% endfor %}
{% endfor %}
I also tried it without the "key" of the variable and only added the namespace, but that did not work either.
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024