Step 1: Set Up Environment Variables
Create a .env
file in your project root directory to store your Shopify API credentials securely:
SHOPIFY_SHOP_NAME=your-shop-name
SHOPIFY_API_KEY=your-api-key
SHOPIFY_API_VERSION=2022-01
Step 2: Install Required Packages
Install the necessary npm packages:
npm install node-fetch dotenv
Step 3: Create the Script for Volume Discount Function
Create a new file volumeDiscount.js
and add the following code:
const fetch = require('node-fetch');
require('dotenv').config();
const SHOPIFY_API_URL = `https://${process.env.SHOPIFY_SHOP_NAME}.myshopify.com/admin/api/${process.env.SHOPIFY_API_VERSION}/graphql.json`;
const ACCESS_TOKEN = process.env.SHOPIFY_API_KEY;
const volumeDiscountFunctionId = "your-function-id"; // Replace with your actual function ID
const volumeDiscountTitle = "Volume discount function";
const createVolumeDiscountMutation = `
mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
automaticAppDiscount {
discountId
}
userErrors {
field
message
}
}
}
`;
const variables = {
automaticAppDiscount: {
title: volumeDiscountTitle,
functionId: volumeDiscountFunctionId,
startsAt: new Date().toISOString(), // Start immediately
}
};
async function createVolumeDiscount() {
const response = await fetch(SHOPIFY_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ACCESS_TOKEN,
},
body: JSON.stringify({
query: createVolumeDiscountMutation,
variables,
}),
});
const data = await response.json();
if (data.errors) {
console.error('Error creating volume discount:', data.errors);
} else if (data.data.discountAutomaticAppCreate.userErrors.length) {
console.error('User errors:', data.data.discountAutomaticAppCreate.userErrors);
} else {
console.log('Volume discount created successfully:', data.data.discountAutomaticAppCreate.automaticAppDiscount.discountId);
}
}
createVolumeDiscount();
Step 4: Run the Script
Execute the script to create the volume discount:
node volumeDiscount.js
Explanation
- Environment Variables: The script uses environment variables to store sensitive information securely.
- GraphQL Mutation: The
discountAutomaticAppCreate
mutation is used to create a new automatic discount function. - Variables: The mutation variables include the title, function ID, and start date of the discount.
- Error Handling: The script checks for errors and user errors returned by the API.
Step 5: Verify the Discount
After running the script, log in to your Shopify admin panel and navigate to the Discounts section to verify that the volume discount has been created successfully.
Summary
This guide provides a step-by-step approach to creating a volume discount using Shopify Functions and the Admin API with GraphQL. Customize the script and variables as needed to fit your specific requirements. By following these steps, you can automate the creation of discounts and manage them efficiently using Node.js and Shopify’s APIs.
Leave a Reply