Skip to main content

How to Create Coupon Codes in WooCommerce via Webhooks and Zapier

This article walks through how to use our webhooks to connect with WooCommerce to automatically create coupon codes in your store

Andre Tan avatar
Written by Andre Tan
Updated yesterday

At this time our integration with WooCommerce does not allow us to create coupon codes in your store for you. This means that if you want to track affiliate sales via coupon code, then you’d need to create a coupon code for your affiliate in Refersion and also create a coupon code with the same name in your WooCommerce store. Using our webhooks and a Zapier account you can automate this process so that when you create a coupon code in Refersion, then a coupon code with the same name will be automatically created for you in WooCommerce.

Please note: this solution requires technical know-how regarding API and webhooks. Also please note this is not an official integration; it is a workaround that makes use of a third party app (Zapier), so it is not something that we can directly assist you with or setup for you. If you are unsure of any of the steps we suggest you consult with your web developer.

You may also need to upgrade to our Scale plan to access webhook notifications from within your Refersion merchant account.

Let’s take a look at catching a webhook notification in Zapier when a new conversion trigger is created. First, you will need to create a Zapier account, and you will need to be on their premium plan in order to work with webhook notifications

Step 1: Create a Zap from scratch

Give your Zap a descriptive name so it would be easier to find later

Step 2: Set up your trigger

In our example, the event we are choosing is when Webhooks by Zapier:

Find “Catch Hook” under Choose Trigger event and hit Continue. On the next page you’ll be given a custom webhook URL. Copy the URL

Step 3: Set up a webhook in Refersion

Next you will have to create a webhook that will send notifications into Zapier. To get started navigate to Account > Settings > Webhooks (on the bottom left) and then click + Create New Webhook.

A new window will pop up. From the popup, choose the New Conversion Trigger topic. Once a topic is selected you’ll enter the custom webhook URL that you copied from Zapier. This is where Refersion will send the webhook data

Click Create. Now you have a webhook that will notify your Zapier account whenever a new conversion trigger is created in Refersion

Step 4 Test Your Zap

Head back to your Zap and click continue to navigate past the custom webhook URL tab. To proceed on the next tab you will have to test your Zap by sending a request to this webhook URL. To test the webhook, just create a new conversion trigger in your account! You can do this by going to the Dashboard>Add Conversion Trigger tab.

Once you have added a new conversion trigger in Refersion you can head back to Zapier and click Test. You may have to wait a few minutes after creating the conversion trigger, but then Zapier should confirm receipt of the webhook, meaning that Refersion is successfully sending data into Zapier whenever a new coupon code is created:

Click the Continue button

Step 5 Add a Filter Step

Since there are a few different types of promotion methods in Refersion (coupon, email, SKU) you will only want your Zap to proceed if the webhook capture is for a coupon code. You can add a filter step in your Zap to account for this:

Then for the filter logic you’ll select the Type field that was captured in the first step from the webhook, exactly matches, COUPON (in call caps):

Test your Zap and confirm that Your Zap would have continued

Step 6: POST Your Coupon Code to WooCommerce

Continue after your filter has been successfully tested. Now we’re finally ready to add the last step, which will push the coupon code from Refersion’s webhook into your WooCommerce store. Let's again choose Webhooks by Zapier for the App.

We’ll be utilizing WooCommerce’s open API to create a coupon code in your WooCommerce store. This is more flexible than WooCommerce’s default integration with Zapier and more accessible (you typically need a specific paid plugin for Woocommerce to work directly with Zapier).

After choosing an App, you will need to choose an Action Event. In this example, we will choose POST and click Continue:

On the next page you will enter your store’s URL followed by /wp-json/wc/v3/coupons in the URL field. For example, if the URL of your store is https://yourawesomestore.com, then you would enter https://yourawesomestore.com/wp-json/wc/v3/coupons

Change the Payload Type from ‘form’ to ‘json’

Next you will be specifying the properties of the coupon codes that you are going to create. Again you can review WooCommerce’s open API to see what are your options. There are a myriad of different properties that you can set for the coupon code that you’re creating in WooCommerce, such as: a description of the coupon, an expiration date for the coupon, a usage limit or a usage limit per customer, minimum and maximum amounts, etc. For our example we will keep the creation settings simple, but we encourage you to explore all of WooCommerce’s available options and adjust your Zap to your needs.

For now we’ll fill out just a few fields. For the first field you will type code on the left, and on the right you’ll be prompted to insert data via a dropdown. You’ll want to select the data from step 1, Catch Hook, and select the ‘Trigger’ field to capture the name of the coupon conversion trigger that was created:

For the next field you will type discount_type on the left. This determines the type of discount that will be applied. Options: percent, fixed_cart and fixed_product. We’ll use percent.

For the next field you will type amount on the left. This is the amount of discount. It should always be numeric, even if setting a percentage. We’ll use ‘10’ to signify that we’re creating 10% off coupon codes.

For the final field you will type individual_use on the left and type TRUE on the right. This makes it so that the coupon code you’re creating cannot be combined with other coupon codes at checkout. This field is REQUIRED for your store’s integration with Refersion (Refersion cannot process orders where multiple coupon codes were used).

Again you are free to use whatever additional fields you want, but for our example this is what all of the fields will look like once they have been specified:

You can leave the next three fields (Wrap Request in Array, File, Unflatten) as is.

For the Basic Auth field you will have to pull a couple of authorization keys from your WooCommerce account. In WooCommerce please navigate to Woocommerce>Settings:

From settings go to Advanced > REST API:

Click Add key. The API key needs read and write permissions:

When you click Generate API key you will be given a Consumer key and a Consumer secret. In Zapier the format that you must use when entering them is pipe separated, so paste your Consumer key into Zapier, type a | character, and paste your Consumer secret into Zapier. You will end up with something like this for example:

ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Finally for the very last section Headers you will type Content-Type on the left and application/json on the right:

Click the Continue button at the bottom, and click Test & Review on the next page. If everything has been setup properly, then your coupon code will have been created for you in your WooCommerce store! You can turn on the Zap if you’re ready, and the creation of coupon codes in your WooCommerce store will be automated for you moving forward.

Did this answer your question?