Get metafields when requesting products return null for all variants

I’m calling the following in C# :

api.Get(“products.json?include=variants,variants.metafields”);

I get the products all right along with the variants but each variant is returned with metafields property to null

despite having set all the variants of the product with the metafields value as shown here :

    public class ShopifyProduct
    {
        public List<Product> Products { get; set; }
    }

    public class Metafield
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
    public class Product
    {
        public long id { get; set; }
        public string title { get; set; }
        public string body_html { get; set; }
        public string vendor { get; set; }
        public string product_type { get; set; }
        public DateTime? created_at { get; set; }
        public string handle { get; set; }
        public DateTime? updated_at { get; set; }
        public DateTime? published_at { get; set; }
        public string template_suffix { get; set; }
        public string status { get; set; }
        public string published_scope { get; set; }
        public string tags { get; set; }
        public string admin_graphql_api_id { get; set; }
        public List<Variant> variants { get; set; }
        public List<Option> options { get; set; }
        public List<Image> images { get; set; }
        public Image image { get; set; }
    }

    public class Variant
    {
        public long? id { get; set; }
        public long? product_id { get; set; }
        public string title { get; set; }
        public string price { get; set; }
        public string sku { get; set; }
        public int? position { get; set; }
        public string inventory_policy { get; set; }
        public object compare_at_price { get; set; }
        public string fulfillment_service { get; set; }
        public object inventory_management { get; set; }
        public string option1 { get; set; }
        public object option2 { get; set; }
        public object option3 { get; set; }
        public DateTime? created_at { get; set; }
        public DateTime? updated_at { get; set; }
        public bool? taxable { get; set; }
        public string barcode { get; set; }
        public int? grams { get; set; }
        public object image_id { get; set; }
        public double? weight { get; set; }
        public string weight_unit { get; set; }
        public long? inventory_item_id { get; set; }
        public int? inventory_quantity { get; set; }
        public int? old_inventory_quantity { get; set; }
        public bool? requires_shipping { get; set; }
        public string admin_graphql_api_id { get; set; }
        public List<Metafield> metafields { get; set; }

    }

string ProductsJson = api.Get("products.json?include=variants,variants.metafields");
ShopifyProduct ShopifyProduct = JsonSerializer.Deserialize<ShopifyProduct>(ProductsJson.Replace("products", "Products"));

1 Like

Hi CubiGear,

I connected with the team that owns the products endpoint, include is not a parameter for that endpoint. It may be applicable to product variant webhook subscriptions. In order to access the product variant metafields you will want to connect to the metafields endpoint as described here. I know that isn’t as nice as querying all variant metafields at once. Hopefully this helps.

Thanks,

-Rick

Hi,

Thank you for your help. It isn’t useful indeed because more requests mean slower response given back to users and fetching let say only 25 orders with each having 2 products which have 4 variants each then going through each variant and call again for metafields. Hmmm My interface has an hour glass forever :disappointed_face: Well, I will have to think about a better design if possible of course.

I have the following code now and I still get empty metafields all the time. What is wrong ?

foreach (Product p in ShopifyProduct.Products)
{
    foreach (Shopify.General.Variant v in p.variants)
    {
        string MetaFieldsResult = api.Get($"metafields.json?metafield[owner_resource]=product_variant&metafield[metafield_id]={v.id}");
        MetafieldRoot MetaFields = JsonSerializer.Deserialize

I'm sadly working to get the metafields for almost 2 days now without success.
1 Like

Hi CubiGear,

I am not set up to run C#, but I ran the following cURL command on your shop for the product “Père Noël”, variant “L/TA” and received the metafields shown after. Note I redacted key parts of the cURL request and response:

curl --request GET
–url https://.myshopify.com/admin/api/2023-01/products//variants//metafields.json
–header ‘content-type: application/json’
–header ‘x-shopify-access-token: ’

Here is a part of the response I recieved:

{
  "metafields": [
    {
      "id": 

Since that worked and I do not have C# set up, I am having a hard time replicating the issue you are having. Perhaps you could send me the request that the C# library is making and we could look at the response from the server?

I know it is frustrating when it is not working, I hope this helps,

-Rick

I appreciate the time you take to help me.

I’ve been able to get the values I need using the following code. The only drawback is the fact that for each variant of the product I have to make a second call to get the data rather than having the metafields with the products I already fetched.

string MetaFieldsResult = api.Get("variants/" + Variant.id.Value.ToString() + "/metafields.json");
MetafieldRoot MetaFields = JsonSerializer.Deserialize

I agree, that is a disadvantage of the REST API. I am sure you have evaluated the GraphQL API and know that would allow for querying in fewer requests. Unfortunately querying all products → all variants → all metafields was blocked due to execution cost. However, I was able to query many variant metafields for a single product like this:


```javascript
query ProductVariantMetafields {
  product(id:"gid://shopify/Product/

But I am sure you have focused on the REST API for good reasons.

Thanks,

-Rick
1 Like

Thank you for your help. All this is valuable.

2 Likes