Shopify: Generate + E-mail Gift Discount Codes

This source lets you easily generate and create Discount Codes for products when they are purchased as gifts
and have them emailed to the customer.
Quick Overview of Final Result:
An order is placed for a gift...
A new Discount Code is generated for gifted product...
Email is sent to customer with Discount Code...
Customer can give this one time use Discount Code to anyone for use...
How It Works
This source, once deployed, listens to orders.create
events from your linked Shopify Store. When a new order is created
we loop through the line items of the order and check if any of them are defined in GIFT_PRODUCT_TO_PRODUCT_MAPPING
in helpers.js
.
You'd need to define a mapping of the gift products IDs to the IDs of the actual products that will be gifted, you can get your
product ID by logging into your Shopify Store Admin Dashboard and clicking the product, it will be in the URL:
Then add the IDs like so:
// Gift Product ID -> Actual Product ID
const GIFT_PRODUCT_TO_PRODUCT_MAPPING = {
1234567890123: 1122334455667,
5152233445566: 4777112233447
};
After the Gift Products have been identified from the line items of the Order, we loop through them, and
generate a new 12 character alphanumeric code that will be used as the Discount Code:
let generatedDiscountCode = crypto.randomBytes(6).toString("hex").toUpperCase();
We then call the products.retrieve
endpoint to fetch
information about the product that will be gifted (mainly the title):
let productId = `gid://shopify/Product/${productIdNumber}`;
// Fetch information about product that will be gifted
let product = await lib.shopify.products['@2020.7.0'].retrieve({
id: `${productId}`
});
Afterwards we create a new one time use 100% off Discount Code for the gifted product with the new code we generated earlier,
using the discountcodebasics.create
endpoint:
// Create a new one time use 100% off discount code for that product
await lib.shopify.discountcodebasics['@2020.7.0'].create({
title: `${product.title} Gift Discount Code (${event.customer.email})`,
startsAt: new Date().toISOString(),
endsAt: null,
usageLimit: 1,
appliesOncePerCustomer: true,
customerGets: {
'value': {
percentage: 1.00 // Get 100% off product
},
'items': {
products: {
productsToAdd: [`${productId}`]
}
}
},
customerSelection: {
'all': true
},
code: generatedDiscountCode
});
And finally once the Discount Code is created, we send it in an email to the customer, using Gmail's messages.create
let customerEmail = event.customer.email || event.email;
// Send email to customer containing the purchased gift Discount Code
await lib.gmail.messages['@0.2.0'].create({
to: `${customerEmail}`,
subject: `Your Gift Product Discount Code`,
html: ejs.render(emailTemplate, {
product: product,
customer: event.customer,
generatedDiscountCode: generatedDiscountCode
})
});
A default email template is used from templates/template.html
, utilizing ejs to render and
handle passing in variables. You can upload your own template to ./templates
and use it instead, make sure you
change line 14 to include your template:
let templateFilename = './templates/your_template.html';
Linking your Shopify Store and Gmail Account
To deploy your own version of this source and connect it to your Shopify Store and Gmail Account, all you need to do is Fork it
by clicking Fork at the top of the screen, once you've created the Fork you'll be brought into the Autocode editor.
A the bottom you'll notice a bouncing red button that says 2 Accounts Required.
Click the button, you see the following screen asking you to link each of Shopify and Gmail:
Click on the Link button for each and Link New Resource and it will walk you through
all the steps to connect your Shopify Store and your Gmail Account:
Deploying Your Source
To start listening to new orders created from your Shopify Store, you need to deploy your source, doing that is as simple as clicking a button!
Find and click the Deploy at the bottom left of the Autocode editor:
Once you press it you'll see a progress screen, and when that's done your source is now live!
That's it!
Thanks for checking out this Source! Hope it helped, if you have any questions please feel free to join our
community Slack channel from the Community Tab in the top bar of the page. You can also follow the Autocode team
on Twitter for updates @AutocodeHQ.