I have two free plugins on the WordPress plugin repository, which let you automatically create discounts for subscribers to your mailing list.
Subscriber Discounts for Easy Digital Downloads, and Subscriber Discounts for WooCommerce. I know, I’m really creative with the names 🙂
Out of the box, these plugins let you create unique discount codes for subscribers to either your MailChimp or ActiveCampaign mailing lists. The plugin uses webhooks from the two services in order to trigger the creation of the discount code. The webhook also fires off an email to the subscriber with the newly created discount code.
These plugins weren’t very flexible
The problem with the first version of these plugins is that it was limited to a MailChimp or ActiveCampaign mailing list. You couldn’t use it with Constant Contact, AWeber, ConvertKit, or any other email marketing platform.
I wanted to make it possible for users to extend the plugins so that it could work with the email marketing platform of their choice.
So, in the latest versions of the plugins, I created extendable classes that only need a little bit of information in order to work with any other platform.
To see how to extend these plugins, here’s the class that creates the MailChimp discount code for Easy Digital Downloads:
class SDEDD_Mailchimp_Create_Discount extends SDEDD_Create_Discount{ /** * Our discount type. Used for type specific filters/actions * @var string * @since 1.1.0 */ public $discount_type = 'mailchimp'; /** * The key used in the webhook. * @return string * @since 1.1.0 */ public function get_key(){ $key = esc_attr( $this->sdedd_options[ 'mailchimp_key' ] ); return $key; } /** * Get email address from the webhook * * @access public * @since 1.1.0 * @return string */ public function get_email() { $email = ''; if( isset( $_POST['data']['merges'] ) ){ $email = wp_strip_all_tags( $_POST['data']['merges']['EMAIL'] ); } if ( ! is_email( $email ) ){ $email = ''; } return $email; } /** * Get contact name from the webhook * * @access public * @since 1.1.0 * @return string */ public function get_name() { $name = ''; if( isset( $_POST['data']['merges'] ) ){ $name = $_POST['data']['merges']['FNAME']; } if ( empty( $name ) ){ $name = $this->sdedd_options['name_placeholder']; } return $name; } } |
This is really all you need to extend the plugin to work with another email platform.
Create your own class
To create a class that extends the base class:
- Find the $_POST data that is sent via the webhook. Your email marketing company likely has that information on their website. You’re looking for the subscriber’s email address and first name. Replace the $_POST data in the relevant functions in the class.
- The $discount_type variable should be equal to the name of the email marketing platform you’re using. Really, this could be anything other than the word ‘default’. This was only a way to keep the base class from running if the type was default.
- Change the $key variable to be equal to the key you use on the webhook.
- Then, change the SDEDD_Mailchimp_Create_Discount class name to your own class name.
- Note: if you’re using the WooCommerce version, change sdedd_options to sdwoo_options.
Update Webhooks
Webhook URLs are structured like this:
http://yourdomain.com/?trigger-special-discount=true&discount-key=xxxxxxxxxxxx (where xxxxxxxxxxxx can be any random set of letters and numbers).
The $key variable in #3 above should be equal to the xxxxxxxxxxxx in your webhook URL.
Then instantiate your class to run.
$create = new SDEDD_Mailchimp_Create_Discount(); $create->create_discount(); |
The base class will check to make sure the webhook is hit correctly before creating discount codes and sending emails to your subscribers.
Hopefully this makes the plugins more flexible, and useful for anyone looking to offer these types of automated discounts.
Let me know if you’ve been able to extend the plugin with any other email marketing platform in the comments.
Leave a Reply