Search functionality not working as per meta field from products

Krishna12
Shopify Partner
42 1 6

Hello,

i am using dawn theme, i want search result as per meta fields of products. added code with predictive-search.liquid, but its not working proper, here is the code, can anyone suggest please.

 

{%- if predictive_search.performed -%}
  <div id="predictive-search-results">
    {%- if predictive_search.resources.products.size > 0 -%}
      <div class="collage-list test">
        {%- assign displayed_colleges = '' -%}

        {%- for collection in predictive_search.resources.products limit: 15 -%}
          {%- assign college_name = collection.metafields.custom.colleges_name.value -%}
          {%- assign college_url = collection.metafields.custom.colleges_name.value | handle -%}
                 
            {%- unless displayed_colleges contains college_name -%}
            <div class="collage-list-item">
              <a class="collage-list-item_inner" href="collections/{{ college_url }}">
                <div class="collage-list-item_img">
                  {% include 'college-icon' %}
                </div>
                <span class="collage-list-item_name">{{ college_name }}</span>
              </a>
            </div>
            
            {%- assign displayed_colleges = displayed_colleges | append: college_name | append: ',' -%}
          {%- endunless -%}
        {%- endfor -%}

      </div>
      {% comment %}
        <a class="view_all" href="/search?q={{ predictive_search.terms }}&options[prefix]=last">
          {{- 'general.search_banner.view_all' | t -}}
        </a>
      {% endcomment %}
    {% else %}
      <p class="empty_data text-center">{{ 'general.search_banner.empty' | t }}</p>
    {%- endif -%}
  </div>
{%- endif -%}

 

@LitCommerce @Cedcommerce @Rishihuptech 

Replies 2 (2)

Rishihuptech
Shopify Partner
470 80 86

Hi @Krishna12 

  1. Loop Variable Naming: You're looping through predictive_search.resources.products but using the variable name collection for each product. This can be misleading since collection typically refers to a group of products. Rename the collection to product for clarity.
  2. Handle Generation: The line assign college_url = collection.metafields.custom.colleges_name.value | handle​ might not work as expected. If colleges_name is a text field, converting it to a handle might not give you a valid URL. Ensure that the handle corresponds to a valid collection or product URL.
  3. Search Indexing: Remember that Shopify's search functionality is based on its indexing. Make sure that the metafields you're using are indexed and searchable.

Here's a revised version of your loop, with some of the above points in mind:

 

 

 

{%- for product in predictive_search.resources.products limit: 15 -%}
  {%- if product.metafields.custom and product.metafields.custom.colleges_name -%}
    {%- assign college_name = product.metafields.custom.colleges_name.value -%}
    {%- assign college_url = college_name | handleize -%}
           
    {%- unless displayed_colleges contains college_name -%}
      <div class="collage-list-item">
        <a class="collage-list-item_inner" href="{{ college_url | url_for_type: 'collection' }}">
          <div class="collage-list-item_img">
            {% include 'college-icon' %}
          </div>
          <span class="collage-list-item_name">{{ college_name }}</span>
        </a>
      </div>
      
      {%- assign displayed_colleges = displayed_colleges | append: college_name | append: ',' -%}
    {%- endunless -%}
  {%- endif -%}
{%- endfor -%}

 

 

 

If these adjustments don't resolve the issue, you should provide more details about what specifically isn't working as expected.

 

 

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required
Krishna12
Shopify Partner
42 1 6

@Rishihuptech 

Thanks for the reponse. i have tried your given solution. but its not working for some reason, sharing you more details. 

attached ss of meta field that i am using for search. 

https://www.loom.com/share/dd91f2119df04d8480e11079ac1080a9

Video attached for result of search. 

Official-merchandise-·-Products-·-Hoodie-·-Shopify.png