How to create and view the metafields in Shopify

agaonkar
Excursionist
33 0 3

Hi Team,

 

I want to create some custom fields into the Shopify Admin application. I read about metafields in the Shopify API documentation. But somehow was not able to understand how to use metafields and where it will get reflected.

I tried to add metafields to products resource and the API call was successful. I was expecting that the new fields which I added would reflect on the Add Products page. But unfortunately it did not appear.

 

So can anyone please help me understand how to go about these metafields?

 

 

Replies 8 (8)

Contrail
Shopify Partner
17 0 2

Hi, you can't view metafields directly. But you can access them using liquid variables.

For example, you might want to add additional information to orders or products such as hotlinks which should be displayed to the customer on their account page. You can edit the customer.account template and display those metafields, e.g.:

 

{% for line_item in order.line_items %}
	{% if line_item.fulfillment and line_item.product.metafields.yournamespace.yourkey %}
		{% for metafield in line_item.product.metafields.yournamespace.yourkey %}
			<tr>
				<td colspan="5">
					<a href="{{ metafield.href }}" target="_blank">
						{{ metafield.href_text }}
					</a>
				</td>
			</tr>
		{% endfor %}
	{% endif %}
{% endfor %}
agaonkar
Excursionist
33 0 3

I want to achieve this via the APIs. Is this possible??

jenn11
Excursionist
33 4 6

Hey @agaonkar , you will need to use a plugin to view and edit the metafields you've assigned in the Add Product page. Metafields Guru is the popular one: https://apps.shopify.com/metafields-editor-2?utm_campaign=installed&utm_content=contextual&utm_mediu...

You are phoenix
agaonkar
Excursionist
33 0 3

@jenn11 

Could you please let me know the steps to achieve the same via APIs.

For example,

Create a new meta field on the Products page

Add some value in that field

View the newly added metafield on the Shopify Admin UI

 

Thanks,

Ambika

agaonkar
Excursionist
33 0 3

Hi,

Any pointers will be highly appreciated.

agaonkar
Excursionist
33 0 3

Basically I would like to know how to use the Metafields API?

jenn11
Excursionist
33 4 6

These documents contains the Metafields API information: 

https://help.shopify.com/en/api/guides/metafields/admin-api-metafields

https://help.shopify.com/en/api/graphql-admin-api/examples/product#creating-products

https://help.shopify.com/en/api/reference/metafield#create-2019-10

 

A sample POST product with metafields would look like this: 

POST /admin/api/2019-10/products.json
//REST
{ "product": { "title": "Burton Custom Freestyle 151", "body_html": "<strong>Good snowboard!</strong>", "vendor": "Burton", "product_type": "Snowboard", "tags": "Barnes & Noble, John's Fav, \"Big Air\"", "metafields": [ { "key": "wax", "value": "Turtle Wax", "value_type": "STRING", "namespace": "snowboard_care_instructions" } [ } }
//graphQL
mutation
($input: ProductInput!) { productUpdate(input: $input) { product {
id,
title, metafields(first: 100) { edges { node { id namespace key value } } } } } }

Once you've uploaded the product with metafields via the API you will need to use or create an app to view/edit them (see my previous link for such an app). 

You are phoenix
agaonkar
Excursionist
33 0 3

Thanks alot for the solution. I am able to proceed with metafields.