Sidra Asghar
Sidra Asghar

Reputation: 89

add metafields to product shopify

I have added product metafields to my shopify and is giving success response but are not displayed on the product details. here is my nodejs code which is giving success response.

const response = await fetch(`https://mystore.myshopify.com/admin/api/2020-07/products/5709040025760/metafields.json`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            "X-Shopify-Access-Token": accessToken,
          },
          body: JSON.stringify({
            "metafield": {
              "namespace": "type",
              "key": "type",
              "value": "test type",
              "value_type": "string"
            }
          })
        })

        const responseJson = await response.json();

Basically, I want is when user/customer click on product there should be a new field in product that is type. Can anyone help me how to display it.

Upvotes: 1

Views: 3560

Answers (1)

Bilal Akbar
Bilal Akbar

Reputation: 4930

The metafields object allows you to store additional information for products, collections, customers, orders, blogs, pages and your shop.

In your case, you have already created Metafield and now just want to display it on product page. To do so, you need to use metafield property on Product object. To access the metafield that your code created, add the following code to product page in Shopify theme.

{{product.metafields.type.type}}

The first type is your namespace while the type after that is field name.

To read more about how to add, edit and display Metafield, follow Shopify Docs for Metafields.

For Shopify Plus account, Yes it is possible to add fields. But you will not be able to access the Shopify form elements in Liquid as those are rendered via tag. However, you can add custom JS file. So on page load, anything is possible.

You can have a look at basic docs for editing checkout.liquid. However, to edit checkout forms and things like that, it is mostly JavaScript with plenty of answers on StackOverflow.

Upvotes: 1

Related Questions