How can I check whether a product variant contains a specific metafield?

Topic summary

A developer is building a theme block that should only display for product variants containing a specific metafield.

Current Approach:

  • Looping through product variants in Liquid to create a list of variants with the target metafield
  • This method has a limitation: only 100 variants can be iterated this way

Challenge:

  • Shopify now supports up to 2000 variants per product, making the current solution inadequate
  • Unable to retrieve metafields for a product variant using the AJAX API

Potential Alternative:

  • Tracking variant changes and requesting variant information via AJAX API using JavaScript

Partial Response:

  • Another user suggests that product.variants in Liquid should support all 2000 variants and can iterate over them using standard Liquid markup in the developer preview

The discussion remains open regarding the best method to handle metafield checks across large variant sets.

Summarized with AI on November 8. AI used: claude-sonnet-4-5-20250929.

I am writing a theme block applicable only to the product template, and I want this block to be visible only for product variants that have a certain metafield. One approach that works for now is to loop through all product variants in Liquid and create a list of variants that have this metafield:

{% assign variants_with_metafield = '' | split: ',' %}

{% for variant in product.variants %}
{% if variant.metafields.namespace.key %}
{% assign variants_with_metafield = variants_with_metafield | push: variant %}
{% endif %}
{% endfor %}

However, as Shopify now supports up to 2000 variants per product, this solution is no longer viable because only 100 variants can be iterated this way. An alternative could be to request information about the variant with the AJAX API by tracking variant changes:


But I can’t find a way to retrieve metafields for a product variant using the AJAX API. Are there any APIs or other methods I could use to implement this functionality?

I am sure the product.variants in liquid will support 2000 variants since on developer preview it can already iterate over 2000 variants using the following

{%for variant in product.variants %}