Creating Discounts Programmatically in Shopify

Hey everyone, I’m trying to figure out if there’s a way to set up discounts automatically in my Shopify store. I know I can do it manually through the admin panel, but I’m wondering if there’s a way to do it with code.

I’ve looked through the Shopify API docs, but I can’t find anything about creating promotions or discount codes. Does anyone know if this is possible? Maybe there’s a way to do it that I’m missing?

If the API doesn’t support this, are there any other methods to create discounts programmatically? I’d rather not have to use cURL requests to the admin if I can avoid it.

Here’s a quick example of what I’m trying to do:

shop = ShopifyAPI::Shop.current
discount = shop.discounts.create(
  code: 'SUMMER2023',
  value_type: 'percentage',
  value: 20,
  starts_at: Time.now,
  ends_at: Time.now + 30.days
)

Any help or suggestions would be really appreciated!

While Shopify’s API doesn’t directly support creating discounts programmatically, there are workarounds. One option is to use the GraphQL Admin API, which allows for more complex operations. You can create price rules and discount codes through this API.

Here’s a basic example using Ruby and the graphql-client gem:

query = <<-GRAPHQL
  mutation {
    discountCodeBasicCreate(
      priceRuleId: "gid://shopify/PriceRule/YOUR_PRICE_RULE_ID",
      code: "SUMMER2023"
    ) {
      discountCode {
        code
      }
      userErrors {
        field
        message
      }
    }
  }
GRAPHQL

result = ShopifyAPI::GraphQL.client.parse(query)
ShopifyAPI::GraphQL.client.query(result)

This approach requires more setup, but it’s more flexible and powerful than the REST API for discount creation. Remember to handle rate limits and errors appropriately in your implementation.

While direct API support for creating discounts is limited, I’ve had success using Shopify’s Bulk Operations API for larger-scale discount creation. It’s not as immediate as a direct API call but has proven effective over time. The process involves preparing a CSV file with all discount details and then uploading it via the Bulk Operations API. You then monitor the process through webhooks or polling the operation status, ensuring any errors are handled promptly. Though it doesn’t provide real-time updates, it’s a reliable alternative to manual discount setup.

In my experience with Shopify, I’ve found that while the API does not directly allow discount creation, a practical workaround involves using the Bulk Operations API. I approached the challenge by first preparing a CSV file containing all the discount details and then uploading this file through the Bulk Operations API. I monitored the progress by either setting up webhooks or polling the operation status. This process, though not as instantaneous as a simple API call and with a slight delay in processing, has reliably supported large-scale discount creation. For real-time requirements, automating interactions with the admin panel via tools like Selenium may be considered, even though it is less elegant.

hey there, i’ve actually found a workaround for this! u can use the shopify script editor to create dynamic discounts. it lets u write ruby code to apply discounts based on cart contents, customer tags, etc. it’s not exactly creating discount codes, but it can automate discounts without needing API calls. might be worth checkin out!