Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

GraphQL PriceRule Value

Solved

GraphQL PriceRule Value

ccoppenbarger
Shopify Partner
5 0 2

We have a need for Bundles on our store, but either the existing Bundle apps don't meet our specific needs, or there's weird bugs such as when you increase the quantity of an item in a bundle, it takes an additional discount off that. Anyways, we're looking at developing our own and in looking at the GraphQL for getting PriceRules, I discovered that either I'm missing something with getting the Value or the Value is not fully fleshed out in the GraphQL API.

 

Here's what I have:

{
  shop {
    name
  }
  priceRule(id: "gid://shopify/PriceRule/324805361728") {
    title
    features
    status
    valueV2 {
      __typename
    }
    itemEntitlements {
      products(first: 3) {
        edges {
          node {
            title
          }
        }
      }
    }
  }
}

And what it returns

"data": {
    "shop": {
      "name": "buymasterbuilt-dev"
    },
    "priceRule": {
      "title": "RB_m2og9qw76c7x3t",
      "features": [],
      "status": "ACTIVE",
      "valueV2": {
        "__typename": "PricingPercentageValue"
      },
      "itemEntitlements": {
        "products": {
          "edges": [
            {
              "node": {
                "title": "MPS 20|B Patio-2-Portable Propane Smoker"
              }
            },
            {
              "node": {
                "title": "#Smokeon T-Shirt"
              }
            }
          ]
        }
      }
    }
  }

Any hints? Thoughts?

Accepted Solution (1)

Zameer
Shopify Staff
297 31 90

This is an accepted solution.

Hey there,

 

It definitely is possible to retrieve the value of each Price Rule using GraphQL, it's just a bit tricky as it requires the use of fragments. 

 

The PriceRule object stores this information in its valueV2 field which is of type PricingValue. The PricingValue object can either be of type MoneyV2 or PricingPercentageValue, which requires you to cast it to the appropriate type using fragments, before accessing the nested percentage or amount value.

 

Below is an example of how you would accomplish this, let me know if you run into any issues!

 

 

{
  priceRules(first: 10) {
    edges {
      node {
        id
        title
        status
        valueV2 {
          __typename
          ... on PricingPercentageValue {
            percentage
          }
          ... on MoneyV2 {
            amount
            currencyCode
          }
        }
      }
    }
  }
}

 

 

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 4 (4)

Zameer
Shopify Staff
297 31 90

This is an accepted solution.

Hey there,

 

It definitely is possible to retrieve the value of each Price Rule using GraphQL, it's just a bit tricky as it requires the use of fragments. 

 

The PriceRule object stores this information in its valueV2 field which is of type PricingValue. The PricingValue object can either be of type MoneyV2 or PricingPercentageValue, which requires you to cast it to the appropriate type using fragments, before accessing the nested percentage or amount value.

 

Below is an example of how you would accomplish this, let me know if you run into any issues!

 

 

{
  priceRules(first: 10) {
    edges {
      node {
        id
        title
        status
        valueV2 {
          __typename
          ... on PricingPercentageValue {
            percentage
          }
          ... on MoneyV2 {
            amount
            currencyCode
          }
        }
      }
    }
  }
}

 

 

To learn more visit the Shopify Help Center or the Community Blog.

ccoppenbarger
Shopify Partner
5 0 2

Hi Zameer,

 

Thanks. That worked great. Thanks for your help. I'm still learning GraphQL and this helped peek into the information I needed.

desney
Visitor
2 0 0

@Zameer I'm getting an error using that query since both PricingPercentageValue and moneyV2 are specified in the same query. How do I get this to work? 

nextlead
Shopify Partner
4 0 2

Might as well throw this in here the query to get specific price rule + discount code,

 

 

{
  priceRule(id: "gid://shopify/PriceRule/964633690287") {
    valueV2 {
      __typename
          ... on PricingPercentageValue {
            percentage
          }
          ... on MoneyV2 {
            amount
            currencyCode
          }
    }
    discountCodes(first:10) {
      edges {
        node {
          id
          code
        }
      }
    }
  }
}

 

 

Which should give you 

{
    "priceRule": {
      "valueV2": {
        "__typename": "PricingPercentageValue",
        "percentage": -10
      },
      "discountCodes": {
        "edges": [
          {
            "node": {
              "id": "gid://shopify/PriceRuleDiscountCode/11477877784751",
              "code": "COOLCODE"
            }
          }
        ]
      }
    }