Skip to content

Instantly share code, notes, and snippets.

@ideadude
Forked from strangerstudios/pmpro_ld2aff.php
Last active January 5, 2022 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/a10279629c2f312f18b90804ce9cd369 to your computer and use it in GitHub Desktop.
Save ideadude/a10279629c2f312f18b90804ce9cd369 to your computer and use it in GitHub Desktop.
Link Paid Memberships Pro discount codes to affiliate accounts from another plugin/service.
<?php
/*
Link discount codes to affiliates.
*/
//this is the parameter we're looking for to find affiliates... based on which plugin/etc you are using
define('PMPRO_LDC2AFF_KEY', 'wpam_id'); //Affiliates Manager Plugin
//define('PMPRO_LDC2AFF_KEY', 'pa'); //WordPress Lightweight Affiliates
//define('PMPRO_LDC2AFF_KEY', 'ref'); //AffiliateWP
//helper function to get the values from options and make sure they are an array
function pmpro_ld2aff_getOptions() {
$d2a = get_option('pmpro_ldc2aff', array());
if(!is_array($d2a))
$d2a = array();
return $d2a;
}
//helper function to add a discount code/affiliate pair
function pmpro_ldc2aff_updateCode($code_id, $affiliate) {
$d2a = pmpro_ld2aff_getOptions();
if(empty($affiliate) && !empty($d2a[$code_id])) {
unset($d2a[$code_id]);
update_option('pmpro_ldc2aff', $d2a, 'no');
} elseif(!empty($affiliate)) {
$d2a[$code_id] = $affiliate;
update_option('pmpro_ldc2aff', $d2a, 'no');
}
}
//helper to get a discount code from an affiliate
function pmpro_ldc2aff_getCodeByAffiliate($affiliate) {
$d2a = pmpro_ld2aff_getOptions();
return array_search($affiliate, $d2a);
}
//helper get an affiliate id from a discount code
function pmpro_ldc2aff_getAffiliateByCode($code_id) {
$d2a = pmpro_ld2aff_getOptions();
if(isset($d2a[$code_id]))
return $d2a[$code_id];
else
return "";
}
//on init we try to sync the affiliate id and discount code
function pmpro_ldc2aff_init() {
global $wpdb;
//if this isn't set, we can't work
if( ! defined( 'PMPRO_LDC2AFF_KEY' ) ) {
return;
}
//make sure PMPro is active
if( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
//might want to check if a specific affiliate plugin is also running
//affiliate id? set discount code
if( ! empty( $_REQUEST[PMPRO_LDC2AFF_KEY] ) ) {
//bail if another discount code is already set
if( ! empty( $_REQUEST['discount_code'] ) ) {
return;
}
//otherwise, let's figure out the code to use
$affiliate = sanitize_text_field($_REQUEST[PMPRO_LDC2AFF_KEY]);
$code_id = pmpro_ldc2aff_getCodeByAffiliate($affiliate);
if(!empty($code_id)) {
$code = $wpdb->get_var("SELECT `code` FROM $wpdb->pmpro_discount_codes WHERE id ='" . intval($code_id) . "' LIMIT 1");
//before we set the code, check if it is valid for this level
if( !empty( $pmpro_level ) ) {
$level_id = $pmpro_level->id;
} elseif ( !empty( $_REQUEST['level'] ) ) {
$level_id = intval( $_REQUEST['level'] );
}
$valid = pmpro_checkDiscountCode( $code, $level_id );
if( $valid ) {
$_REQUEST['discount_code'] = $code;
$_GET['discount_code'] = $code;
$_POST['discount_code'] = $code;
//prevent caching of this page load
add_action( 'send_headers', 'nocache_headers' );
return;
}
}
}
//discount code? set affiliate id
if( ! empty( $_REQUEST['discount_code'] ) ) {
$code = sanitize_text_field($_REQUEST['discount_code']);
$code_id = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE `code` = '" . esc_sql($code) . "' LIMIT 1");
if(!empty($code_id)) {
$affiliate = pmpro_ldc2aff_getAffiliateByCode($code_id);
$_REQUEST[PMPRO_LDC2AFF_KEY] = $affiliate;
$_GET[PMPRO_LDC2AFF_KEY] = $affiliate;
$_POST[PMPRO_LDC2AFF_KEY] = $affiliate;
//prevent caching of this page load
add_action( 'send_headers', 'nocache_headers' );
return;
}
}
}
add_action('init', 'pmpro_ldc2aff_init', 1);
//show options on discont code page
function pmpro_ldc2aff_pmpro_discount_code_after_settings($code_id) {
$affiliate = pmpro_ldc2aff_getAffiliateByCode($code_id);
?>
<hr />
<h3>Affiliate</h3>
<p>Link this discount code to an affiliate. Currently monitoring the '<?php echo PMPRO_LDC2AFF_KEY;?>' parameter. When the affiliate ID is set, this discount code will be defaulted to be used at checkout, and when a discount code is used the affiliate ID will be set.</p>
<p><label for="pmpro_ldc2aff_affiliate">Affiliate ID:</label><input type="text" id="pmpro_ldc2aff_affiliate" name="pmpro_ldc2aff_affiliate" value="<?php echo esc_attr($affiliate);?>" /></p>
<hr />
<?php
}
add_action('pmpro_discount_code_after_settings', 'pmpro_ldc2aff_pmpro_discount_code_after_settings');
//save options when code is updated
function pmpro_ldc2aff_pmpro_save_discount_code($code_id) {
if($code_id < 1)
return;
$affiliate = sanitize_text_field($_REQUEST['pmpro_ldc2aff_affiliate']);
pmpro_ldc2aff_updateCode($code_id, $affiliate);
}
add_action('pmpro_save_discount_code', 'pmpro_ldc2aff_pmpro_save_discount_code');
//delete options when code is deleted
function pmpro_ldc2aff_pmpro_delete_discount_code($code_id) {
if($code_id < 1)
return;
pmpro_ldc2aff_updateCode($code_id, false);
}
add_action('pmpro_delete_discount_code', 'pmpro_ldc2aff_pmpro_delete_discount_code');
@ideadude
Copy link
Author

ideadude commented May 1, 2018

When I forked this, I added a check to make sure that the discount code was valid for the selected level and not expired before applying the code at checkout. Otherwise, users wouldn't be able to checkout. They would get the invalid code error.

@createwebsitenet
Copy link

Hello i am trying to link but how to use this code do we have to install as plugin ? please give some direction or guidlines

@ideadude
Copy link
Author

ideadude commented Jan 5, 2022

Late reply. But for others, yes, you need to add this into a custom plugin. Alternatively, you can use something like the "Code Snippets" plugin and add this code as a code snippet.

https://www.paidmembershipspro.com/how-to-add-code-to-wordpress/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment