MU Affiliate work – Adding MLM aspect to affiliate program

Affiliate Multi level tree totals
Affiliate report area

Made several minor tweak to enable a virtually unlimited size down line /  commission system

Added tree view. This will display grand totals of all the members that are in your down line.

In the admin area I added a option to set the amount of levels to pay on.

From there this menu will generate the appropriate amount of boxes that can be used to set the percentage to be paid on each level.

2 Levels Deep

Then 9 levels deep

Supporter with 9 levels of commission

In order to store the data efficiently I created this table.


– Table structure for table `wp_affiliate_tree`

CREATE TABLE `wp_affiliate_tree` (
`user_id` bigint(20) NOT NULL,
`ref_user_id` bigint(20) NOT NULL,
`lvl` mediumint(6) NOT NULL,
PRIMARY KEY  (`user_id`,`ref_user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=’affiliate support tree’;

Upon a user activation I added a hook to build the new users full tree.

This function activates the new user as a affiliate and builds there tree

function initial_register_affiliate($user_id, $password, $met){
$user_info=get_userdata($user_id);
$reference = $user_info->user_login. ‘-’ . strrev(sprintf(‘%02d’, $user_id + 35));
update_usermeta($user_id, ‘enable_affiliate’, ‘yes’);
update_usermeta($user_id, ‘affiliate_reference’, $reference);
update_usermeta($user_id, ‘affiliate_hash’, ‘aff’ . md5(AUTH_SALT . $reference));
$refer_user_id=$user_info->affiliate_referrer;
// If referred by another user_id then build upline tree
if($refer_user_id){

global $wpdb;
$sql = $wpdb->prepare( “INSERT IGNORE INTO “.$wpdb->base_prefix.”affiliate_tree (user_id, ref_user_id, lvl) VALUES (%d, %d, 1)”, $user_id,$refer_user_id );
$queryresult = $wpdb->query($sql);

$sql = $wpdb->prepare( “INSERT IGNORE INTO  “.$wpdb->base_prefix.”affiliate_tree (user_id, ref_user_id, lvl)
SELECT %d, ref_user_id, lvl+1 FROM “.$wpdb->base_prefix.”affiliate_tree WHERE  user_id=%d”, $user_id, $refer_user_id );
$queryresult = $wpdb->query($sql);
}
}

From there I tweaked the supporter-amazon.php and paypal file as there was  a bug that was causing the affiliate payment process not to load.

Also on the affiliate program  there were some other random fixes. I worked the code regarding a user showing up on a user page without a referralid.

It now cross references the blog that they landed on with adminstrator of the blog. It uses that data to set the cookie.

There was a problem with the cookie being set by the ‘ref’ get query.. However at that point in the code it was not defined.

if(defined(‘AFFILIATE_CHECKALL’)) {
// We are here if there isn’t a reference passed, so we need to check the referrer.
if(!isset( $_COOKIE['affiliate_' . COOKIEHASH]) && isset($_SERVER['HTTP_REFERER'])) {
// We haven’t already been referred here by someone else – note only the first referrer
// within a time period gets the cookie.
$referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
global $wpdb;

// Check if the user is a valid referrer
//$affiliate = $this->db->get_var( $this->db->prepare( “SELECT user_id FROM {$this->db->usermeta} WHERE meta_key = ‘affiliate_referrer’ AND meta_value=’%s’”, $referrer) );
$affiliate = $this->db->get_var( $this->db->prepare( “SELECT b.user_id FROM ” . $wpdb->base_prefix . “blogs a,  ” . $wpdb->base_prefix . “bp_user_blogs b WHERE  a.blog_id=b.blog_id AND a.domain=’%s’ LIMIT 1″, $referrer) );

// Check for mapped domain.
if(!$affiliate){
$affiliate = $this->db->get_var( $this->db->prepare( “SELECT b.user_id FROM ” . $wpdb->base_prefix . “domain_mapping a,  ” . $wpdb->base_prefix . “bp_user_blogs b WHERE  a.blog_id=b.blog_id AND a.domain=’%s’ LIMIT 1″, $referrer) );
}

if($affiliate) {
$_GET['ref']=$this->db->get_var( $this->db->prepare( “SELECT meta_value FROM {$this->db->usermeta} WHERE meta_key = ‘affiliate_reference’ AND user_id=’%s’”, $affiliate) );
// Update a quick count for this month
do_action( ‘affiliate_click’, $affiliate);
// Store the referrer
do_action( ‘affiliate_referrer’, $affiliate, $referrer );

// Write the affiliate hash out – valid for 30 days.
@setcookie(‘affiliate_’ . COOKIEHASH, ‘aff’ . md5(AUTH_SALT . $_GET['ref']), (time() + (60*60*24*30)), COOKIEPATH, COOKIE_DOMAIN);
} else {
if(defined(‘AFFILIATE_SETNOCOOKIE’)) @setcookie(‘noaffiliate_’ . COOKIEHASH, ‘notanaff’, 0, COOKIEPATH, COOKIE_DOMAIN);
}
}
}

4 thoughts on “MU Affiliate work – Adding MLM aspect to affiliate program”

  1. Hi, I am building a multisite and I have ran into a problem. I was wondering if you could help me. I am using membership and affiliates from wpmudev.org.
    I really need the affiliates to work with membership, but most importantly, I need the payments for my affiliates to be residual. Do you have any ideas on how I can achieve this?

    I have tried all kinds of membership scripts and I am really hitting a brick wall at this point. I am hopeful the membership and affiliate plugin can do what I need it to do.

    Thanks

    1. I would check to make sure PayPal subscriptions are being correctly renewed. For example the supporter mod has a PayPal gateway script that gets pinged every month by Paypal for each renewal. If that is not working, the commission will fail. Some server side caching technologies can block the successful operation of the script. Therefore, it is important to setup exceptions for those scripts.
      Honestly, I went through the affiliate plugin line by line debugging it with my setup and making changes in order to get it working with our solution. The version I am using now is customized rewrite.

  2. Hello -

    Joseph, based on your last statement (There was a problem with the cookie being set by the ‘ref’ get query.. However at that point in the code it was not defined.) I am wondering, does this code work or are you still working out the bugs?

    1. Hi Crystal,
      I am using the code, and it is working. However, these are some rather old changes since WPMUDev has released many revisions since then. They have probably fixed many of these issues if not already rewrote much of the code for this project. If I had to do it again I would stick with the solutions they had rather then customizing it. Once you customize you are committing to a endless workload of code maintenance whenever you want to update wordpress. This is of course unless maintaining this type of customization is your job :-)

Leave a Reply