Solved

How can I get the automatic discount code applied in response of Cart Query using Storefront API

GkPayments
Shopify Partner
8 0 1

I am using this query

{
              	id
							discountAllocations {
								discountedAmount {
									amount
									currencyCode
								 }
								}
							discountCodes{
								applicable
								code
							}

			attributes {
				key
				value
			}
			cost {
				totalAmount {
					amount
					currencyCode
				}
				subtotalAmount {
					amount
					currencyCode
				}
				totalTaxAmount {
					amount
					currencyCode
				}
				totalDutyAmount {
					amount
					currencyCode
				}
			}
			totalQuantity
			note
        lines(first:100){
            edges{
                node{
                    id
                    merchandise{
                         ... on ProductVariant {
                id
                title
								product{
									createdAt
									description
									id
									productType
									title
									updatedAt
									vendor
								}
								image{
									height
									id
									url
									width
								}
								price
								unitPrice{
									amount
									currencyCode
								}
              }
                    }
                    quantity

                }
            }
        }}}}

Where and what would I have add to this query to receive the name of the automatic discount code applied on the cart?
I read the docs about CartAutomaticDiscountAllocation but can't figure out how to access/ reach this object.

Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1317 216 454

This is an accepted solution.

Hi @GkPayments 👋

 

Since `CartDiscountAllocation` is an interface, you can use an inline fragment on `CartAutomaticDiscountAllocation` like this:

query ($id: ID!) {
  cart(id: $id) {
    id
    discountAllocations {
      ... on CartAutomaticDiscountAllocation {
        title
        discountedAmount {
          currencyCode
          amount
        }
      }
    }
  }
}

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Reply 1 (1)

ShopifyDevSup
Shopify Staff
1317 216 454

This is an accepted solution.

Hi @GkPayments 👋

 

Since `CartDiscountAllocation` is an interface, you can use an inline fragment on `CartAutomaticDiscountAllocation` like this:

query ($id: ID!) {
  cart(id: $id) {
    id
    discountAllocations {
      ... on CartAutomaticDiscountAllocation {
        title
        discountedAmount {
          currencyCode
          amount
        }
      }
    }
  }
}

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog