How to Create Automatic Discounts in Shopify Remix Using GraphQL?

How to Create Automatic Discounts in Shopify Remix Using GraphQL

Generating automatic discounts via code enhances Shopify app developers’ ability to provide dynamic, personalized promotion strategies, automatically. Developers that are using Shopify Remix and the Admin GraphQL API can create automatic discounts (i.e “$10 off $100 purchase”) programmatically, right from their own custom admin UI and/or backend logic.

Shopify Development Services

Why Use GraphQL for Discounts?

The GraphQL Admin API is the most straightforward way to create, retrieve and manage discounts, especially on scale. It gives you the ability to:

  • Remove repetitive manual setup
  • Dynamically apply discounts 
  • Build custom admin interfaces or automation

What We’re Building

We will build a GraphQL mutation that creates an automatic discount:

“Get $10 off on orders over $100, combinable with shipping discounts.”

Step-by-Step Setup in Shopify Remix

Authenticate the Admin API

In your app using Shopify Remix, authenticate the Admin API with authenticate.admin(request), here’s how it works:

import { authenticate } from "../shopify.server";

export async function action({ request }: ActionArgs) {
  const { admin } = await authenticate.admin(request);
  
  const response = await admin.graphql(
    `#graphql
    mutation discountAutomaticBasicCreate($automaticBasicDiscount: DiscountAutomaticBasicInput!) {
      discountAutomaticBasicCreate(automaticBasicDiscount: $automaticBasicDiscount) {
        automaticDiscountNode {
          id
          automaticDiscount {
            ... on DiscountAutomaticBasic {
              title
              startsAt
              combinesWith {
                productDiscounts
                shippingDiscounts
                orderDiscounts
              }
              minimumRequirement {
                ... on DiscountMinimumSubtotal {
                  greaterThanOrEqualToSubtotal {
                    amount
                    currencyCode
                  }
                }
              }
              customerGets {
                value {
                  ... on DiscountAmount {
                    amount {
                      amount
                      currencyCode
                    }
                  }
                }
                items {
                  ... on AllDiscountItems {
                    allItems
                  }
                }
              }
            }
          }
        }
        userErrors {
          field
          code
          message
        }
      }
    }`,
    {
      variables: {
        automaticBasicDiscount: {
          title: "$10 off orders over $100 (combinable with shipping discounts)",
          startsAt: new Date().toISOString(),
          minimumRequirement: {
            subtotal: {
              greaterThanOrEqualToSubtotal: "100.00"
            }
          },
          customerGets: {
            value: {
              discountAmount: {
                amount: "10.00",
                appliesOnEachItem: false
              }
            },
            items: {
              all: true
            }
          },
          combinesWith: {
            productDiscounts: false,
            shippingDiscounts: true,
            orderDiscounts: false
          }
        }
      }
    }
  );

  const data = await response.json();

  if (data.data.discountAutomaticBasicCreate.userErrors.length > 0) {
    console.error("Discount creation error:", data.data.discountAutomaticBasicCreate.userErrors);
    return { success: false, errors: data.data.discountAutomaticBasicCreate.userErrors };
  }

  return { success: true, discountId: data.data.discountAutomaticBasicCreate.automaticDiscountNode.id };
}

What This Code Does?

  • Authenticates the admin API with authenticate.admin(request)
  • Runs the discountAutomaticBasicCreate mutation
  • Passes all required discount fields, including:

Title

Start date

Discount value and rules

Combination settings

  • Returns the discount Id, if there is an error it returns the error

Testing the Discount

  1. For testing, either deploy your Remix app or run it locally
  2. Submit the form or trigger the mutation via your custom admin route
  3. Check the Shopify Admin → Discounts section to see the new automatic discount

Required Permissions

Be sure your app has the following Admin API scopes:

  • write_discounts
  • read_discounts

You can set this in your app’s shopify.app.toml or admin UI.

Conclusion

In Shopify Remix, implementing discounts using GraphQL provides unmatched reign and automation. You can create a UI where store admins define their rules or run it conditionally through server-side logic.

This setup is perfect for:

  • Custom promotions
  • Automated sales campaigns
  • Discount A/B testing systems
Hire Shopify Developers

FAQ

  1. Can I set up automatic discounts in Shopify without using GraphQL or any code?

      Absolutely! You can set up basic automatic discounts from the Shopify Admin dashboard. When creating discounts this way you will have limitations related to automatic discounts compared to code-based automatic discounts using GraphQL and Shopify Functions.

      2. How do I add an automatic discount on Shopify without GraphQL?

        You are able to set up automatic discounts from the Admin section of your Shopify store.

        Steps:

        • Go to Shopify Admin > Discounts
        • Click Create Discount
        • Select Automatic Discount
        • Set the following:

        Discount name

        Type: Percentage, Fixed amount, Buy X Get Y

        Conditions: Product, quantity, or order value

        Start & end date

        • Click Save

        3. Can I have more than one automatic discount applied at the same time?

          No. Shopify limits you to one automatic discount per customer to use at a time – it does not matter if the discount was created in the admin section or programmatically.

          Previous Article

          Magento 2: How to Create a Category using Data Patch

          Next Article

          How to Create a User with Laravel’s firstOrCreate Method?

          View Comments (1)

          Leave a Comment

          Your email address will not be published. Required fields are marked *

          Get Connect With Us

          Subscribe to our email newsletter to get the latest posts delivered right to your email.
          Pure inspiration, zero spam ✨