Re: How do to access 'Category Metafields' in Liquid?

How do to access 'Category Metafields' in Liquid?

indextwo
Shopify Partner
16 1 7

We've only just seen the newer 'Category Metafields' when creating a product in Shopify:

 

Screenshot 2024-07-05 at 13-45-27 Tire Streets wepixel Development · Products · Tire Streets Garage Workshop Banner - Green · Shopify.png

My question is: how do I access the value of this in Liquid? It would be incredibly handy to determine some display functionality based on the product category. But I can't find anything in the docs or online. Is it simply part of

product.metafields

...but with a standardised name?

n00b
Replies 7 (7)

Maurice1420
Shopify Partner
2 0 0

Dieing to know aswell. Can't be so difficult to provide users with a basic doc upon adding new features right?

indextwo
Shopify Partner
16 1 7

I had a response from Shopify Support when I emailed them about this, and currently it's not accessible via Liquid (or any other means, AFAIK).

They have said they might consider it as they can see the usefulness, but currently it's not possible.

n00b
jamesauble
Shopify Partner
13 0 7

Looks like i'm seeing them being used on live themes. How are they rendering them? via APIs?

Maurice1420
Shopify Partner
2 0 0

I believe, dispite the feedback from shopify, that the values can actually be accessed through a "product_option_value". I only managed to do it with the option Color so far. Maybe its just that this was specifically linked somehow. Anyway. I had to add color swatches to a customer store and could access them through the product_option_value.swatch.color

It looks somewhat like this:

{% unless collection.handle == "fussmatte" %}
            <div class="hkt_swatches_container">
            {%- for option in card_product.options_with_values -%}
              {%- for value in option.values -%}
                {% assign variant = value.variant %}
                
                {% if option.name == "Farben" or option.name == "Colors" or option.name == "Color" or option.name == "Farbe" %}
                    <div class="hkt_swatch_outside">                      
                        <a class="hkt_swatch" href="{{ variant.url }}" style="background-color:{{ value.swatch.color }};">
                        </a>
                    </div>            
                {% endif %}
              {% endfor %}
            {% endfor %}
          </div>
          {% endunless %}

 

Hotmann
Shopify Partner
3 0 0

Thanks so much for this 🙏. Worked perfectly after over 24 hours trying to find a solution to it.

ineptian
Shopify Partner
5 0 0

Maurice unless I am not following, the above code only returns the product options, it has nothing to do with the category metafields.

AubV60
Shopify Partner
1 0 0

You can assign a product to a category and then a list of category metafields will become available on the product.

You can access them in the same way as standard metafields, but using the shopify namespace.

To see the namespace, key and return type (in this case metaobject (list)), check the relevant metafield definition here:
https://admin.shopify.com/store/<store name>/settings/custom_data/product/metafields

 

Example:

{% liquid
assign firstProduct = collections["all"].products.first
assign sizeMetafield = firstProduct.metafields.shopify.size.value
%}
<script>
console.log("productId", {{ firstProduct.id }});
console.log("sizeMetafield", {{ sizeMetafield | json }});
</script>

 There is some confusion in these comments around options, which is perhaps because you can link these category metafields to the product options, and they would therefore display the same name, but options and category metafield values are not necessarily linked.