Need some help with Metafields in Search and a Theme Code insertion

Need some help with Metafields in Search and a Theme Code insertion

SoCalMacGuy
Shopify Partner
31 0 21

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 

Replies 19 (19)

EBOOST
Shopify Partner
1398 351 431

Hi @SoCalMacGuy ,

If you use Dawn theme. May I suggest to update code these steps:

1. Go to Store Online-> theme -> edit code

EBOOST_1-1672356985580.png

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 %}

EBOOST_1-1713771059844.png

 

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 %}

EBOOST_2-1713771144524.png

 

 

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
SoCalMacGuy
Shopify Partner
31 0 21

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.

breaddddd
Shopify Partner
18 0 5

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.

EBOOST
Shopify Partner
1398 351 431

Hi @breaddddd ,

It is only work for free themes. If you use a fee theme. We need check structure that theme to insert code.

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
breaddddd
Shopify Partner
18 0 5

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.

EBOOST
Shopify Partner
1398 351 431

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 %}

EBOOST_0-1729089286675.png

EBOOST_1-1729089311408.png

 

 

 

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
breaddddd
Shopify Partner
18 0 5

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.

LAHerb
Tourist
8 0 1

Could you please tell us where to put this on our theme? Couldn't find this line on our main-search but I know it'll work on our theme if we can figure it out. Thanks! Using Canopy

EBOOST
Shopify Partner
1398 351 431

Hi @LAHerb ,

Canopy theme is not the free theme. So I  don't know the structure of theme. Could you please let me access your store?

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
LAHerb
Tourist
8 0 1

Yeah please, could you request access or what should I invite?

EBOOST
Shopify Partner
1398 351 431

Could you share store url and code to sent a request?

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
LAHerb
Tourist
8 0 1

la herb . com

 

Thanks!

EBOOST
Shopify Partner
1398 351 431

Hi,

I send a request. Could you please help to accept?

EBOOST_0-1742341530807.png

 

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
LAHerb
Tourist
8 0 1

I accepted, thanks!

EBOOST
Shopify Partner
1398 351 431

Hi @LAHerb ,

the duplicate featured has been disabled. So I worked on LA Herb theme. You can preview on this theme. If it is ok then you can make do it for live theme. https://laherb.com/?_ab=0&_fd=0&_sc=1&preview_theme_id=149180514557

EBOOST_2-1742362764963.png

 

EBOOST_0-1742362744476.pngEBOOST_1-1742362751242.png

 

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
LAHerb
Tourist
8 0 1

Not sure it's working? I type in some of the metafield options for alt product names and nothing comes up with that link?

EBOOST
Shopify Partner
1398 351 431

Hi,

This code only work for 20 products in all products, single text type, and namespace: custom. If you use multiple type. You need  use an app.

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free. Github.com/eboost10
- ❤❤DONATE ❤❤Coffee tips
LAHerb
Tourist
8 0 1

I'm not sure I understand? We are looking for the metafield info to come up in search. We use alternative product names as some items have multiple names. These multiple names are not coming up in search since they are in metafield. I thought this code fixed that?

LAHerb
Tourist
8 0 1

Sorry missed code part it is 9453. Please let me know so I can change it after. Thanks!