-
-
Save litonarefin/402c9c559b03687da42bf3dd1a84f161 to your computer and use it in GitHub Desktop.
Automatically email a discount code to new ActiveCampapign subscribers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: EDD - Discounts for Subscribers | |
* Plugin URI: https://www.nosegraze.com/subscribers-edd-discount-code | |
* Description: Automatically email a discount code to new ActiveCampaign subscribers. | |
* Version: 1.0 | |
* Author: Nose Graze | |
* Author URI: https://www.nosegraze.com | |
* License: GPL2 | |
* | |
* @package edd-discounts-subscribers | |
* @copyright Copyright (c) 2016, Nose Graze Ltd. | |
* @license GPL2+ | |
* | |
* INSTRUCTIONS: | |
* | |
* This plugin isn't meant to be installed as-is. You need to edit it quite a bit to | |
* get it working with your settings, name, email, etc. | |
* | |
* First you need to set up a webhook in ActiveCampaign in the "Automations" section. | |
* My trigger is when a new person subscribes to my mailing list, but you can choose any | |
* trigger you want. Then the action should be a webhook. Have it POST data to this URL: | |
* http://yoursite.com/?trigger-special-discount=true&discount-key=JkbYRHvWAoddmVUTK1u8RH8V | |
* Replace that 'discount-key' value with your own unique string of numbers and letters. You'll | |
* need to enter that again in the code below. | |
* | |
* Then comb through the plugin and edit any of the values to suit your needs. | |
*/ | |
// Exit if accessed directly | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
/** | |
* Create and send 10% off coupon code when signing up for mailing list. | |
* | |
* @since 1.0 | |
* @return void | |
*/ | |
function ng_edd_discounts_subscribers_send_code() { | |
// Note: Change the 'discount-key' value to your own unique key. | |
if ( ! isset( $_GET['trigger-special-discount'] ) || ! isset( $_GET['discount-key'] ) || $_GET['discount-key'] != 'JkbYRHvWAoddmVUTK1u8RH8V' || ! function_exists( 'edd_store_discount' ) ) { | |
return; | |
} | |
// Now check to make sure we got data from AC. | |
if ( ! isset( $_POST['contact'] ) || ! isset( $_POST['contact']['email'] ) ) { | |
return; | |
} | |
$contact = $_POST['contact']; | |
$email = wp_strip_all_tags( $contact['email'] ); | |
// There's no reason it wouldn't be an email, but just in case... | |
if ( ! is_email( $email ) ) { | |
return; | |
} | |
global $wpdb; | |
/* | |
* This is the name that will appear in the admin panel for your reference only. You can change it if you want. | |
* The two % signs are intentional. Only one will show up though. | |
*/ | |
$discount_name = sprintf( '10%% off for %s', $email ); | |
$query = " | |
SELECT * | |
FROM $wpdb->posts | |
WHERE $wpdb->posts.post_title LIKE '$discount_name%' | |
AND $wpdb->posts.post_type = 'edd_discount' | |
ORDER BY $wpdb->posts.post_title | |
"; | |
$results = $wpdb->get_results( $query ); | |
// Already created a discount for this email - bail. | |
if ( is_array( $results ) && count( $results ) ) { | |
return; | |
} | |
$timestamp = time(); | |
$numbers_array = str_split( $timestamp . rand( 10, 99 ) ); | |
$letters_array = array_combine( range( 1, 26 ), range( 'a', 'z' ) ); | |
$final_code = ''; | |
foreach ( $numbers_array as $key => $value ) { | |
$final_code .= $letters_array[ $value ]; | |
} | |
// Arguments for the discount code. You can edit these. | |
$discount_args = array( | |
'code' => $final_code, | |
'name' => $discount_name, | |
'status' => 'active', | |
'max' => 1, // Max number of uses. | |
'amount' => 10, // Dollar or percentage amount to deduct. | |
'type' => 'percent', // 'percent' or 'flat' | |
'use_once' => true, // Only one use per customer. Kind of irrelevant if you chose '1' for max use above, but whatevs. | |
); | |
edd_store_discount( $discount_args ); | |
// If they entered a first name, use that. Otherwise use "there". | |
$first_name = ( array_key_exists( 'first_name', $contact ) && ! empty( $contact['first_name'] ) ) ? $contact['first_name'] : 'there'; | |
// Actual welcome email. You probably want to edit this. | |
$message = sprintf( | |
"<p>Hey %s!</p> | |
<p>Thanks so much for signing up for my list. As a thank you, here's a 10%% off discount code you can use on your next purchase in my shop:</p> | |
<p><strong>%s</strong></p> | |
<p>Enter this code at checkout to remove 10%% from your total.</p> | |
<p>Have a great day!</p> | |
<p>- Your Name</p>", | |
esc_html( $first_name ), | |
esc_html( $final_code ) | |
); | |
$headers = array( | |
'Content-Type: text/html; charset=UTF-8', | |
'From: Your Name <[email protected]>' // Be sure to change this to your name/email. | |
); | |
// Subject line is the second bit in quotes. | |
wp_mail( $email, '10% off Discount', $message, $headers ); | |
} | |
add_action( 'init', 'ng_edd_discounts_subscribers_send_code' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment