Woocommerce + Mailchimp Create One Time Discount Coupon For New Subscribers

Maté Gvo
4 min readAug 20, 2019

Buckle up, I am going to walk you through on how to create a one time discount coupon for your newsletter subscibers, using Mailchimp and Woocommerce.

Overview

When a user signs up, we want to create a one-time-use coupon in WooCommerce, with a discount of 10%, as an incentive for our shop visitors to sign up to our newsletter.

Getting it done

First of all, you will need to create a MailChimp list and a sign up form. Here’s some reference on how to do this: https://mailchimp.com/help/add-a-signup-form-to-your-website/

Part I Mailchimp

Now, when the user’s signs up, we want MailChimp to inform WooCommerce that a coupon needs to be created. To achieve that, we need to create a webhook. We can do it programaticallg via MailChimp API or manually. Second option is easier, and we need to do it only once, so let’s stick to that:

  1. Navigate to Audiance
  2. Click on “Manage Audiance” button and choose “settings”

3. Scroll down, choose “Webhooks”

4. Choose “Create new webhook”.

In the options, you will be asked for Callback URL. Put something like “https://example.com/wp-json/webhook/newMailChimpSubscriber” replacing example.com with the domain of your WooCommerce shop. I will explain in the next step how to handle this.

`/wp-json/` tells Wordpress to use API. API is designed for software to communicate. In our case it’s MailChimp and WooCommerce.

Deselected all type of updates, except for “Subscribes”. You can leave “only send updates (…)” to “By a subscriber” and “By an account admin”.

Save and forget. Now we need to add code that will be waiting for mailchimp to open the callback URL we have just assgined “wp-json/webhook/newMailChimpSubscriber”.

Part II Woocommerce

Open your theme functions.php file. You can read more about it here but in short, it’s the right place to add custom code to your website.

First we will register the new endpoint like this:

add_action('rest_api_init', function () {
// here we are telling wordpress to use "webhook" namespace.
// This could be anything, but since it's a custom webhook receiver,
// it makes sense to call it webhook
// next is our "newMailChimpSubscriber" endpoint or route name
register_rest_route('webhook', '/newMailChimpSubscriber/', array(
'methods' => ['POST','GET'],
// and this is name of the function that will be called,
// when our /wp-json/webhook/newMailChimpSubscriber/ endpoint is called
'callback' => 'createDiscountCouponForNewSubsciber',
));
});

So now we have our endpoint. But we need to actually create the one-time use coupon. Notice above we specified “callback” function. Let’s define it:

// this function creates the coupon, for the newly registered user
function createDiscountCouponForNewSubsciber()
{
// create coupon
}

Now we need logic inside our callback function, that will create the coupon:

$coupon_code = 'testcoupon'; // Code
$amount = '10'; // Amount
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product

$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);

$new_coupon_id = wp_insert_post($coupon);

// Add meta
update_post_meta($new_coupon_id, 'discount_type', $discount_type);
update_post_meta($new_coupon_id, 'coupon_amount', $amount);
update_post_meta($new_coupon_id, 'individual_use', 'no');
update_post_meta($new_coupon_id, 'product_ids', '');
update_post_meta($new_coupon_id, 'exclude_product_ids', '');
update_post_meta($new_coupon_id, 'usage_limit', '1');
update_post_meta($new_coupon_id, 'expiry_date', '');
update_post_meta($new_coupon_id, 'apply_before_tax', 'yes');
update_post_meta($new_coupon_id, 'free_shipping', 'no');

return "ok";

I copied and edited the above code from this gist. We can now test whether the coupon will be created. To do that, open your command line and call the endpoint like this:

$ curl --insecure -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST https://fy.com/fineyellow/wp-json/webhook/newMailChimpSubscriber

CURL is a command that allows to create requests. It’s same kind of a request that MailChimp will send to our Wordpress. This is how it should look:

notice “ok” in the last line — we got the response from our callback function, so our endpoint is working, and we can see that our coupon was created

Finally, we want to adjust the coupon code, so that it’s individual for our new subscriber. We are going to capture users’s email address to do that. We will capture email address from MailChimp response. Edit the first lines of our function:

$email = $_POST['data']['email'];
$coupon_code = 'welcome-'.$email; // Code

Voila, we are ready for testing. Sign up to your newsletter, and see if the coupon is created!

IMPORTANT — (If your coupon wasn’t created) at the last step of writing this tutorial, I realised that MailChimp is checking if the endpoint we specified exists. Since we first made the webhook and than the endpoint, the settings wouldn’t save. Please go back to your mailchimp webhook settings, and make sure you don’t get red message under endpoint url:

--

--