Struggling to Access Multiple Metafields in Shopify Discount Functions

Solved

Struggling to Access Multiple Metafields in Shopify Discount Functions

yedla_dinesh
Shopify Partner
13 0 3

I’m currently facing an issue with accessing multiple metafields inside Shopify discount functions. Using the discountAutomaticAppCreate GraphQL mutation, I created 2 automatic discounts with the same appId  and same metafields namespace, resulting in two discounts, each containing metafield data.  Each discount has its own metafield data, but the keys are different while sharing the same namespace.

When I try to access a single metafield like this:

discountNode {
    metafield(namespace: "test", key: "test-key") {
      value
    }
  }


It retrieves the value by specifying the metafield key. However, since the keys are different, I’m struggling to retrieve both metafields within the same namespace.

yedla_dinesh_0-1734443052375.png


However, I’m unable to retrieve these metafield values within the Shopify discount functions. Below is the input.graphql code I’ve been working with.

query GetCartContents($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
          product {
            id
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
  }
  discountNode {
    metafield(namespace: "test", key: "test-key") {
      value
    }
  }
}

If anyone has insights or solutions for this, I’d greatly appreciate your help!

Shopify Expert
Helping thousands of @Shopify store owners to achieve sustainable growth & increase revenue 2-3x 
Accepted Solution (1)

tobebuilds
Shopify Partner
501 35 132

This is an accepted solution.

You can easily handle this problem by giving an alias to each metafield query:

 

discountNode {
    metafield1: metafield(namespace: "test", key: "test-key-1") {
      value
    }
    metafield2: metafield(namespace: "test", key: "test-key-2") {
      value
    }
  }

 

Best,

Tobe

 

Founder, Regios Discounts app (4.8 stars, 77 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer

View solution in original post

Replies 2 (2)

tobebuilds
Shopify Partner
501 35 132

This is an accepted solution.

You can easily handle this problem by giving an alias to each metafield query:

 

discountNode {
    metafield1: metafield(namespace: "test", key: "test-key-1") {
      value
    }
    metafield2: metafield(namespace: "test", key: "test-key-2") {
      value
    }
  }

 

Best,

Tobe

 

Founder, Regios Discounts app (4.8 stars, 77 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer
yedla_dinesh
Shopify Partner
13 0 3

Hi Tobe,

Thanks for your reply, really appreciate it.

I’ve tried the same approach, but it’s not working as expected. It does return a metafield value, but not both. Sometimes it returns the value of metafield1, and sometimes it returns metafield2.

Here's the output:

"discountNode": {
    "metafield1": null,
    "metafield2": {
      "value": "{\"title\":"Test metadata",\"products\":[]}"
    },
  },

 

and here's the input.graphql file code

 

query GetCartContents($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
          product {
            id
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
  }
  discountNode {
    metafield1: metafield(namespace: "test", key: "test-key") {
      value
    }
    metafield2: metafield(namespace: "test", key: "test-key-1") {
      value
    }
  }
}

 


Let me know if I’m doing anything wrong.

Thanks
Dinesh

Shopify Expert
Helping thousands of @Shopify store owners to achieve sustainable growth & increase revenue 2-3x 