Metafields not showing up in product after API call

Hi,

I’m trying to add a product to Shopify using the ShopifyAPI Python package.

I’m using the following code:

shop_url = "https://%s:%s@1946de-3.myshopify.com/admin" % (config('SHOPIFY_KEY'), config('SHOPIFY_SECRET'))
shopify.ShopifyResource.set_site(shop_url)

new_product = shopify.Product()
new_product.title = product.title
new_product.body_html = product.description
new_product.vendor = str(product.stockrecords.first().partnernew_product.product_type = product.categories.first().name
new_product.images = [{'src': STATIC_BASE_URL + str(item.original)} for item in images]new_product.status = 'active'
new_product.metafields = [
    {
        'key': 'extra_informatie',
        'value': '{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"Dit is extra info!"}]}]}',
        'type': 'rich_text_field',
        'namespace': 'custom'
    }
]
success = new_product.save()

variant = new_product.variants[0]
variant.price = price
variant.taxable = True
variant.sku = product.stockrecords.first().partner_sku
variant.inventory_management = 'shopify'
variant.inventory_policy = 'continue'
success_variant = variant.save()

Everything is showing up fine in the Shopify admin, but the metafields remain empty.

What am I doing wrong?

Hi Luukmaas,

I haven’t come across developers using the library to create metafields yet, so I’m not 100% sure if it does support metafield creation. Is the metafield definition already set up on the store for the metafield you’re looking to add to a product?

Hi Luukmaas,

You might not be seeing the metafield appear on the product if the metafield definition doesn’t already exist. You’ll likely need to create the metafield definition (either via API or in the admin) before creating the product and it’s associated metafield value.

Hope this helps!

I already added the definition (through the admin), but it doesn’t add values through the API…

Hi again Luukmaas,

It’s possible that the creating/ or setting of metafields is not fully supported by this library. Could you test out using this GraphQL mutation to create a metafield on a product to see if that works? You can use the GraphiQL app with products scope read and write as an interface to create the metafield.