How can I display dynamic product variants metafields on a product page?

I’m at that exact spot too - I’m using variant fields to show a ‘back in stock’ message when the person clicks on the variant swatch.

i have it working well when all variations have data in the field but when one doesn’t, it keeps showing the data from the last click.

My one thought is in the JS i’m using to have a ‘if is null’ kind of statement, but i can’t get it working.

Here’s my code:

{%- if product.metafields.product.stock_info != blank -%} //This tells it to use the main Product field if it exists and ignore variant fields
        {{ product.metafields.product.stock_info }}

      {%- else -%}
      
 //This is the main block to show the info, its hidden first, then turned to 'block' when activated.
          {% capture meta_data %}
          	{% for variant in product.variants %}
                {%- if variant.metafields.variant.stock_date != blank -%}
                  	{{ variant.id}}:{{variant.metafields.variant.stock_date | json}}
              	{%- endif -%}
    		{% unless forloop.last %},{% endunless %}
          	{% endfor %}
          {% endcapture %}
    
       

          {%- endif -%}