Custom filters based on metafield don't work for Storefront API

skatkov
Visitor
1 0 0

As of 2022-01 release Shopify supports custom filters based on metafields, but support is still severely limited to certain field types. Since announcement was made in a storefront api github repo, my assumption is that it should work in storefront api as well.

 

I've created product metafield with supported type.

Screenshot 2022-03-25 at 17.25.57.png


Filled it with data and created custom filter based on this metafield.

Screenshot 2022-03-25 at 17.29.10.png


But when I query API for available filters -- I receive all the standard filters you would expect, yeat no custom filters based on product metafields.

 

 

 

# POST here https://**.myshopify.com/api/2022-01/graphql.json
query Facets {
  collection(handle: "all") {
    handle
    products(first: 10) {
      filters {
        id
        label
        type
        values {
          id
          label
          count
          input
        }
      }
    }
  }
}

 

 

 


I'm not sure, why is it not working? Are any extra steps required that I'm not aware of? Or Storefront API support is not included for custom filters?

Replies 12 (12)

_JCC_
Shopify Staff
200 27 55

Hi @skatkov, have you tried the filter query on a smaller collection where you know the first ten products in the collection have a value for the metafield you're using in the filter you created? If the first ten products didn't have a metafield value set on the product when querying the filters it would not show up as an available filter. I also just wanted to add that the storefront api currently only supports metafield filtering with metafield types single_line_text_fieldnumber_integer, and number_decimal as of API version 2022-01

John C | Developer Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

PeteCass
Visitor
2 0 0

@_JCC_ I'm taking this over from @skatkov  on our side.  In the query above instead of asking for the first 10 products I have updated the query to ask for the first 250 products. This is all of the products in the collection. Still the result is the same as it was previously. 

_JCC_
Shopify Staff
200 27 55

Hey @PeteCass do you have an x-request-id from the response headers of the StoreFront API request where no filters are being returned? With that I can take a look at our logs and see if there's anything that jumps out at as a possible cause. The initial release of faceted filtering is online store dependent. Even though you're using the Storefront API are the products in that collection available on the online store?

John C | Developer Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

PeteCass
Visitor
2 0 0

@_JCC_ Thanks, The request ID is 3b9eaf2e-c6f7-42dd-951f-78ea691652ba

 

The products should be available on the online store

_JCC_
Shopify Staff
200 27 55

For the community, I just wanted to follow up with a key point about faceted collection filtering with the Storefront API. 

 

Currently, the faceted filtering is tightly coupled to the Shopify online store channel, and only products published to that channel will be available to use in collection filtering. In the case of a headless or custom storefront implementation the products need to be published to the app used for the custom storefront along with the online channel.

John C | Developer Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

BramK
Tourist
4 0 1

Had the same problem, but actually forgot the click it in the online store filtering! Works now!

ndrtek
Shopify Partner
12 0 9

Hi.

Is there documentation showing an example of filtering via metafield value via the Storefront API?

This page does not mention it.

But this page makes it look / sound like it should be possible.

As do the comments in this thread.

TY!

G

j-riv
Shopify Partner
10 1 0

@ndrtek  Here are the docs for querying products by metafield value. Although it does not seem to work for me.

 

https://shopify.dev/api/examples/filter-products#query-products-by-metafield-value

_JCC_
Shopify Staff
200 27 55

Hey @j-riv I recently added the metafield example to the docs so I'm interested in finding out why it might not be working for you. Have all of the below been verified?

 

1. The products are published to the online store channel.

2. The metafield filter has been added as an available filter under Online store->Navigation->Collection and Search Filters.

3. The metafield types are any of numeric_decimal,numeric_integer,boolean,single_line_text_field.

4. You're using API version 2022-04 or higher.

 

If it's a case of some metafield filter types work and others do not please let me know.

John C | Developer Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

j-riv
Shopify Partner
10 1 0

Hey @_JCC_ I had missed step 2, Is that somewhere in the docs? I had never seen the `Collection and search filters` section under navigation, that's new to me. Thank you, the query is working as expected.

_JCC_
Shopify Staff
200 27 55

Hey @j-riv perhaps we can do better with the wording but there's a link in the requirements section on the filters product page to our online help that identifies setting up the custom filters.

 

  • You've verified that an Online Store 2.0 compatible theme, such as Dawn, is installed by checking if your theme supports filtering. The Online Store 2.0 theme does not need to be the published theme to enable product filtering.

 

John C | Developer Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

policenauts
Shopify Partner
207 10 67

@_JCC_ Hi John, we are testing querying products by either product or variant metafield for a merchant with 95k products in their collection, and unfortunately it's not working. When the product is early in the collection (e.g., in the first 100) it returns the data. However, we start trying to query for products listed later on and we get an empty result. We have modified our query to do products(first:100000) but to no avail. 

 

The first couple of times we attempted this we got an internal server error (request ID c59c0693-562f-4bc2-b0eb-7534d43d0b27). Afterward we no longer got the error, but it would return empty data for any products that were high in the collection. 

 

Any ideas or workarounds? Thank you.