Shopify Liquid Get Product Object From Metafield

August 3rd, 2022





When setting a metafield to a type “product” you would be forgiven for thinking it would return a product object when accessed.

Unfortunately, this is Shopify so it returns a String GID.

It took me much longer than I would like to admit to figure this out. It was the reason I couldn’t access any of the product’s variables.

To access the object replace the following:

card_product: product.metafields.namespace.featured_product

with

card_product: product.metafields.namespace.featured_product.value

or use the all products array:

card_product: all_products[product.metafields.namespace.featured_product]

Appending .value loads the object rather than returning Shopify’s gid:// string.

Alternatively, we can use the all_products array to access the products by GID.


Comments

5 thoughts on "Shopify Liquid Get Product Object From Metafield"

  1. Richard says:

    This is fantastic, thank you

  2. sub says:

    thanks it really helped

    1. joeybab3 says:

      For sure, glad to hear it!

  3. German says:

    Hi,
    Where do you change that in Dawn theme?

    1. joeybab3 says:

      Wherever you want, anywhere you need a products metafield to be outputted

Leave a Reply

Your email address will not be published. Required fields are marked *