How to read metaobject that is populated with API

Solved

How to read metaobject that is populated with API

pramodraam
Shopify Partner
77 2 9

I created a new metaobject since none of the listed store areas like Products, variants and so on in Shopify admin fit my requirement. The metaobject I created contains two fields as seen in the image below.

 
 

image (8).png

 

I want to know how to read these fields in header.liquid.

 

I need to change site's header logo often and want to make it automated with a cronjob that runs on a different server that populates these fields once a day with the logo file name. 

 

Accepted Solution (1)

NomtechSolution
Astronaut
1245 113 160

This is an accepted solution.

To read the fields from your custom metaobject in the header.liquid file, you'll need to use Shopify's Liquid language and access the metafields associated with the current shop.

Here's an example of how you can retrieve and display the values of the custom metaobject fields in your header:

{% assign shop_metaobject = shop.metafields.custom.my_metaobject_key %}

{% if shop_metaobject %}
  {% assign logo_file_name = shop_metaobject.logo_file_name %}
  {% assign logo_alt_text = shop_metaobject.logo_alt_text %}

  <img src="{{ logo_file_name }}" alt="{{ logo_alt_text }}" />
{% endif %}

View solution in original post

Replies 2 (2)

NomtechSolution
Astronaut
1245 113 160

This is an accepted solution.

To read the fields from your custom metaobject in the header.liquid file, you'll need to use Shopify's Liquid language and access the metafields associated with the current shop.

Here's an example of how you can retrieve and display the values of the custom metaobject fields in your header:

{% assign shop_metaobject = shop.metafields.custom.my_metaobject_key %}

{% if shop_metaobject %}
  {% assign logo_file_name = shop_metaobject.logo_file_name %}
  {% assign logo_alt_text = shop_metaobject.logo_alt_text %}

  <img src="{{ logo_file_name }}" alt="{{ logo_alt_text }}" />
{% endif %}
pramodraam
Shopify Partner
77 2 9

Thank you. Can you tell me what is difference between metaobject and metafields as I see them both in Shopify admin?