GraphQL PriceRule Value

Solved
ccoppenbarger
Tourist
3 0 1

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?

0 Likes
Zameer
Shopify Staff
Shopify Staff
295 32 74

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
          }
        }
      }
    }
  }
}

 

 

ccoppenbarger
Tourist
3 0 1

Hi Zameer,

 

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

0 Likes
desney
New Member
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? 

0 Likes