So, I’ve hacked together a simple addon for directory. I’m having trouble with it accounting for the actual purchase and I’m not sure why, because according to https://wpmudev.com/blog/integrating-the-affiliate-plugin-part-one/ I have all of the parts covered.
As you’ll see, I use the existence of the role in the user_id as proof that it is a paid user, as it’s not set until the paypal transaction is completed.
This gets the role:
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
$rolecheck = $role;
}
if($rolecheck != 'directory_member_paid') $rolevalid = false; //check the role, if not paid then mark rolevalid as false
And then when setting affiliate status I check to be sure that rolevalid isn’t false.
if(function_exists('get_user_meta') && $rolevalid != false) {
$aff = get_user_meta($user_id, 'affiliate_referrer', true);
$paid = get_user_meta($user_id, 'affiliate_paid', true);
} else {
$aff = get_usermeta($user_id, 'affiliate_referrer');
$paid = get_usermeta($user_id, 'affiliate_paid');
}
I hooked in to wpmu_new_user so they both run on user creation. Maybe someone can clue me in?
It’s tracking unique clicks but not tracking signups or paid subscriptions. It succesfully enables inside of the affiliate plugin settings and does not get in the way of account creation.
<?php
/*
Plugin Name: Directory add-on
Description: Affiliate system plugin for the WordPress Supporter plugin
Author: Kruzen
Author URI: http://nwgnetworks.com
*/
add_action('wpmu_new_user', 'affiliate_new_user', 10, 2);
add_action( 'wpmu_new_user', 'affiliate_new_paiduser', 10, 4 );
function affiliate_new_user( $user_id ) {
// Call the affiliate action
do_action( 'affiliate_signup' );
if(defined( 'AFFILIATEID' )) {
// We found an affiliate that referred this new user
if(function_exists('update_user_meta')) {
update_user_meta($user_id, 'affiliate_referrer', AFFILIATEID);
} else {
update_usermeta($user_id, 'affiliate_referrer', AFFILIATEID);
}
}
}
function affiliate_new_paiduser( $user_id ) {
//Get User's Role
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
$rolecheck = $role;
}
if($rolecheck != 'directory_member_paid') $rolevalid = false; //check the role, if not paid then mark rolevalid as false
if(function_exists('get_user_meta') && $rolevalid != false) {
$aff = get_user_meta($user_id, 'affiliate_referrer', true);
$paid = get_user_meta($user_id, 'affiliate_paid', true);
} else {
$aff = get_usermeta($user_id, 'affiliate_referrer');
$paid = get_usermeta($user_id, 'affiliate_paid');
}
if(empty($aff)) $aff = false;
if($aff && $paid != 'yes') {
$amount = 1; //set affiliate amount
do_action('affiliate_purchase', $aff, $amount);
if(defined('AFFILIATE_PAYONCE') && AFFILIATE_PAYONCE == 'yes') {
if(function_exists('update_user_meta')) {
update_user_meta($user_id, 'affiliate_paid', 'yes');
} else {
update_usermeta($user_id, 'affiliate_paid', 'yes');
}
}
}
}
?>