Core Functionalities
Discount Class

Discount Class

The Discount class provides a clean and efficient way to work with Shopify discounts, including both automatic discounts and discount codes. This class encapsulates various operations such as creating, updating, activating, deactivating, and deleting discounts, as well as managing related metafields.

Table of Contents

  1. Installation
  2. Usage
  3. Methods
  4. Examples

Installation

To use the Discount class, make sure you have the necessary Shopify Admin API access. Then, import the class from your project's location:

import { Discount } from './path/to/entities/discount';

Usage

Create an instance of the Discount class by passing the Shopify Admin API client:

const discount = new Discount(admin);

Methods

Automatic Discounts

createAutomatic(options)

Creates a new automatic discount.

Parameters:

  • options: An object containing:
    • title: String
    • functionId: String
    • startsAt: Date (default: current date)
    • endsAt: Date (optional)
    • metafields: Array (optional)
    • combinesWith: Object (optional)

updateAutomatic(options)

Updates an existing automatic discount.

Parameters:

  • options: An object containing:
    • id: String
    • title: String
    • startsAt: Date
    • endsAt: Date (optional)
    • metafields: Array (optional)
    • combinesWith: Object (optional)

deactivateAutomatic(automaticDiscountId)

Deactivates an automatic discount.

activateAutomatic(automaticDiscountId)

Activates an automatic discount.

deleteAutomatic(automaticDiscountId)

Deletes an automatic discount.

queryAutomaticDiscounts()

Retrieves a list of automatic discounts.

Discount Codes

createCode(options)

Creates a new discount code.

Parameters:

  • options: An object containing:
    • appliesOncePerCustomer: Boolean
    • title: String
    • code: String
    • usageLimit: Number
    • functionId: String
    • startsAt: Date (default: current date)
    • endsAt: Date (optional)
    • metafields: Array (optional)
    • combinesWith: Object (optional)

updateCode(options)

Updates an existing discount code.

Parameters:

  • options: An object containing:
    • id: String
    • title: String
    • appliesOncePerCustomer: Boolean
    • code: String
    • usageLimit: Number
    • startsAt: Date
    • endsAt: Date (optional)
    • metafields: Array (optional)
    • combinesWith: Object (optional)

deleteCode(discountId)

Deletes a discount code.

deactivateCode(discountId)

Deactivates a discount code.

activateCode(discountId)

Activates a discount code.

Metafields

setAutomaticMetafields(discountId, data)

Sets metafields for an automatic discount.

getAutomaticWithMetafield(id, metafieldKey, metafieldNamespace)

Retrieves an automatic discount with its metafield.

getCodeWithMetafield(id, metafieldKey, metafieldNamespace)

Retrieves a discount code with its metafield.

Examples

Creating an Automatic Discount

const shopifyAdminApiClient = // ... initialize Shopify Admin API client
 
const discount = new Discount(shopifyAdminApiClient);
 
const result = await discount.createAutomatic({
  title: "Summer Sale",
  functionId: "YOUR_FUNCTION_ID",
  startsAt: new Date(),
  endsAt: new Date(new Date().setMonth(new Date().getMonth() + 1)),
  combinesWith: {
    orderDiscounts: false,
    productDiscounts: true,
    shippingDiscounts: false
  }
});
 
console.log(result);

Updating a Discount Code

const updatedDiscount = await discount.updateCode({
  id: "gid://shopify/DiscountCodeNode/12345",
  title: "Updated Winter Sale",
  code: "WINTER2024",
  usageLimit: 1000,
  appliesOncePerCustomer: true,
  startsAt: new Date(),
  endsAt: new Date(new Date().setMonth(new Date().getMonth() + 3))
});
 
console.log(updatedDiscount);

Querying Automatic Discounts

const discounts = await discount.queryAutomaticDiscounts();
console.log(discounts);

Managing Discount Metafields

const discountId = "gid://shopify/DiscountAutomaticNode/67890";
const metafieldData = { key: "value" };
 
await discount.setAutomaticMetafields(discountId, metafieldData);
 
const discountWithMetafield = await discount.getAutomaticWithMetafield(
  discountId,
  "function-configuration",
  "discount"
);
 
console.log(discountWithMetafield);

This documentation provides a comprehensive guide to using the Discount class for managing Shopify discounts. It covers all the available methods with their parameters and includes practical examples to help developers get started quickly.