Automatically applying discount code on checkout

wafflehorse
Tourist
5 0 1

Hi there!

 

I'm currently building an app and I'm planning on adding a feature that will automatically apply a discount to specific customers via a savedSearch. I'm achieving this by creating a PriceRule and an associated DiscountCode. I know similar questions have been asked in this forum, but I'm not seeing a very clear answer.

 

How would I go about automatically applying a discount code at checkout? Could I have a checkout/create webhook that then sends a checkout mutation with the discount code? Is it possible to apply a discount code to the cart? 

 

I've seen answers involving DraftOrders. But I'm not completely sure about the differences between an order and a draft order.

 

Thank you for your help!

Parker

Replies 4 (4)

Busfox
Shopify Staff (Retired)
628 49 110

Hi @wafflehorse,

 

In the 2019-07 version, you can create an automatic discount using our GraphQL mutation found here. If your app is a sales channel and is creating checkouts through the checkout API, you can programatically add discounts when creating checkouts. That said, it sounds like the above mutation would serve you better.

 

Let me know if you have further questions.

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

wafflehorse
Tourist
5 0 1

Thanks for the reply! I'll try out that mutation and let you know if I have any issues.

 

Cheers!

wafflehorse
Tourist
5 0 1

I have a follow-up question. A detail about the discount that I'm trying to apply is that it should only be valid for certain customers (identified by a tag). Can this automatic discount have specific customer conditions? I know that I can do this with discount codes so that's what I'm currently using.

 

Thank you!

linq-dev
Tourist
11 1 2

Hi, I am working on a similar thing with automatic discounts but when using the graphql mutation I am receiving this error.

 

Error: GraphQL error: Internal error. Looks like something went wrong on our end.
Request ID: 54284b8b-d710-4e94-b7a6-222318277275 (include this in support requests).

 

This is my mutation call -

export const ADD_AUTO_DISCOUNT = gql`
mutation PostAutoDiscount($perentage: Decimal!, $productIds: [String!]) {
    discountAutomaticBasicCreate(automaticBasicDiscount: {
        title: "AutoDiscountTest"
        startsAt: ${new Date().getTime()}
        customerGets: {
          value: {
           percentage: $percentage
            }
          items: {
            products: {
              productsToAdd: $productIds
            }          
          }
        }
        minimumRequirement: {
          quantity: {
            greaterThanOrEqualToQuantity: "2"
          }
        }
    })
    {
      userErrors { field message code }
      automaticDiscountNode {
          automaticDiscount {
          ... on DiscountAutomaticBasic {
            title
            shortSummary
            summary
            minimumRequirement {
              ... on DiscountMinimumQuantity { quantity: greaterThanOrEqualToQuantity }
              ... on DiscountMinimumSubtotal { subtotal: greaterThanOrEqualToSubtotal { amount currencyCode }}
            }
          }
        }
      }
    }
  }`

I am seeing that others are having issues with this as well. Is this a problem on shopify's end?