wordpress plugin help

Hi i need to add one function in my plugin for get 3rd tier referrer details. Below is the sample code for 2nd tier and i need same like for 3rd tier.

Function explanation.

I (A) refer (B) and if (B) made any sale I will get direct commission for that sale (1st tier)

Now (B) refers © and if © made any sale (B) will get direct commission for that sale (1st tier) and I(A) will get tier commission for that sale (2nd tier)

Now © refers (D) and if (D) made any sale © will get direct commission for that sale (1st tier) and (B) will get tier commission for that sale (2nd tier) and I also should get tier commission for that sale (3rd tire).

Up to 2nd tier there is a function available in that plugin and now i need to add a function for 3rd tier.

Here is the sample code which is working fine for 2nd tire.

function wp_aff_award_second_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name=’’)
{
global $aff_tx_msg;
$clientdate = (date (“Y-m-d”));
$clienttime = (date (“H:i:s”));

	if (get_option('wp_aff_use_2tier') && !empty($wp_aff_affiliates_db->referrer))
	{
		$aff_tx_msg .= '<br />Using tier model';
		wp_affiliate_log_debug("Using tier model",true);
		$award_tier_commission = true;	
		$duration = get_option('wp_aff_2nd_tier_duration');		
		if(!empty($duration))
		{
			$join_date = $wp_aff_affiliates_db->date;
			$days_since_joined = round((strtotime(date("Y-m-d")) - strtotime($join_date) ) / (60 * 60 * 24));
			
			if ($days_since_joined > $duration)
			{
				$aff_tx_msg .= '<br />Tier commission award duration expried';
				wp_affiliate_log_debug("Tier commission award duration expried! No tier commission will be awarded for this sale.",true);
				$award_tier_commission = false;
			}
		}				
		if ($award_tier_commission)
		{
			if(!empty($wp_aff_affiliates_db->sec_tier_commissionlevel)){
				$second_tier_commission_level = $wp_aff_affiliates_db->sec_tier_commissionlevel;
				wp_affiliate_log_debug("Using the affiliate specific 2nd tier commission for this referral. 2nd tier commission level: ".$second_tier_commission_level,true);
			}
			else{
				$second_tier_commission_level = get_option('wp_aff_2nd_tier_commission_level');
				wp_affiliate_log_debug("Using global 2nd tier commission for this referral. 2nd tier commission level: ".$second_tier_commission_level,true);
			}
			if (get_option('wp_aff_use_fixed_commission'))
			{
                $commission_amount = $second_tier_commission_level;
            }
            else
            {
			    $commission_amount = round(($second_tier_commission_level * $sale_amount)/100,2);
            }
            $campaign_id = "";
            $is_tier_comm = "yes";
			global $wpdb;
			$aff_sales_table = WP_AFF_SALES_TBL_NAME;
			$updatedb = "INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id,buyer_name,is_tier_comm) VALUES ('$wp_aff_affiliates_db->referrer','$clientdate','$clienttime','','','$commission_amount','$sale_amount','$txn_id','$item_id','$buyer_email','$campaign_id','$buyer_name','$is_tier_comm')";
			$results = $wpdb->query($updatedb);	
			$aff_tx_msg .= '<br />Tier commission awarded to: '.$wp_aff_affiliates_db->referrer.'. Commission amount: '.$commission_amount;
			wp_affiliate_log_debug('Tier commission awarded to: '.$wp_aff_affiliates_db->referrer.'. Commission amount: '.$commission_amount,true);	
		}			
	}	
	return $aff_tx_msg;

}

Hi

Just check the below functions. first function is awarding commission for direct referral and second function is awarding commission for 2nd tier referral and third function is added by me for 3rd tier commission but it is not worked bcoz it is not getting the parent referral details. So pleas guide me how to get the parent referral function.

function wp_aff_award_commission($referrer,$sale_amount,$txn_id,$item_id,$buyer_email,$clientip=’’,$comm_rate=’’,$buyer_name=’’)
{
global $aff_tx_msg,$aff_error_msg;
$commission_award_result = “”;
$debug_data = “”;
$debug_data .= "Referrer: ".$referrer. ", Sale Amount: “.$sale_amount.”, Transaction ID: “.$txn_id.”, ";
$debug_data .= "Item ID: ".$item_id. ", Buyer Email: “.$buyer_email.”, Custom Commission Rate: ".$comm_rate;
$aff_tx_msg = $debug_data;
wp_affiliate_log_debug($debug_data,true);
$clientdate = (date (“Y-m-d”));
$clienttime = (date (“H:i:s”));
if(empty($clientip))
{
$clientip = $_SERVER[‘REMOTE_ADDR’];
}
if(empty($txn_id))
{
$txn_id = uniqid();
}
if (!empty($referrer))
{
global $wpdb;
$affiliates_table_name = $wpdb->prefix . “affiliates_tbl”;
$aff_sales_table = $wpdb->prefix . “affiliates_sales_tbl”;
$wp_aff_affiliates_db = $wpdb->get_row(“SELECT * FROM $affiliates_table_name WHERE refid = ‘$referrer’”, OBJECT);
if(empty($comm_rate)){
$commission_level = $wp_aff_affiliates_db->commissionlevel;
}
else{
$commission_level = $comm_rate;
}

if (get_option(‘wp_aff_use_fixed_commission’))
{
$commission_amount = $commission_level;
}
else
{
$commission_amount = ($commission_level * $sale_amount)/100;
}

if(WP_AFFILIATE_NO_COMMISSION_FOR_SELF_PURCHASE == ‘1’){
if(!empty($buyer_email)){
if(wp_aff_check_if_buyer_is_referrer($referrer,$buyer_email)){
wp_affiliate_log_debug(‘The buyer (’.$buyer_email.’) is the referrer (’.$referrer.’) so this sale is NOT ELIGIBLE for generating any commission.’,true);
return true;
}
}
else{
wp_affiliate_log_debug(“Buyer email data is missing from the request so the plugin cannot verify the WP_AFFILIATE_NO_COMMISSION_FOR_SELF_PURCHASE option”,true);
}
}

$commission_amount = round($commission_amount,2);
$c_id=’’;
if(isset($_COOKIE[‘c_id’])){
$c_id = $_COOKIE[‘c_id’];
}

    if(!empty($buyer_name)){

$updatedb = “INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id,buyer_name) VALUES (’$referrer’,’$clientdate’,’$clienttime’,’’,’$clientip’,’$commission_amount’,’$sale_amount’,’$txn_id’,’$item_id’,’$buyer_email’,’$c_id’,’$buyer_name’)”;
}
else{
$updatedb = “INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id) VALUES (’$referrer’,’$clientdate’,’$clienttime’,’’,’$clientip’,’$commission_amount’,’$sale_amount’,’$txn_id’,’$item_id’,’$buyer_email’,’$c_id’)”;
}
$results = $wpdb->query($updatedb);
if(!$results)
{
$aff_tx_msg .= "
The database update query failed for table: ".$aff_sales_table;
wp_affiliate_log_debug("The database update query failed for table: ".$aff_sales_table,false);
}
else
{
$aff_tx_msg .= '
The sale has been registered in the WP Affiliate Platform Database for referrer: ‘.$referrer.’ with amount: '.$commission_amount;
wp_affiliate_log_debug('The sale has been registered in the WP Affiliate Platform Database for referrer: ‘.$referrer.’ with amount: '.$commission_amount,true);
}
wp_aff_send_commission_notification($wp_aff_affiliates_db->email,$txn_id);

// 2nd tier commission
$commission_award_result = wp_aff_award_second_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);

//This below lines are added by me for tiers 3 to 7
// 3rd tier commission
$commission_award_result = wp_aff_award_third_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);
// 4th tier commission
$commission_award_result = wp_aff_award_fourth_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);
// 5th tier commission
$commission_award_result = wp_aff_award_fifth_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);
// 6th tier commission
$commission_award_result = wp_aff_award_sixth_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);
// 7th tier commission
$commission_award_result = wp_aff_award_seventh_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);
// Up to above i added in this function
}
return $commission_award_result;
}

//This function is existing and working fine for second tier

function wp_aff_award_second_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name=’’)
{
global $aff_tx_msg;
$clientdate = (date (“Y-m-d”));
$clienttime = (date (“H:i:s”));

if (get_option(‘wp_aff_use_2tier’) && !empty($wp_aff_affiliates_db->referrer))
{
$aff_tx_msg .= ‘
Using tier model’;
wp_affiliate_log_debug(“Using tier model”,true);
$award_tier_commission = true;
$duration = get_option(‘wp_aff_2nd_tier_duration’);
if(!empty($duration))
{
$join_date = $wp_aff_affiliates_db->date;
$days_since_joined = round((strtotime(date(“Y-m-d”)) - strtotime($join_date) ) / (60 * 60 * 24));

if ($days_since_joined > $duration)
{
$aff_tx_msg .= ‘
Tier commission award duration expried’;
wp_affiliate_log_debug(“Tier commission award duration expried! No tier commission will be awarded for this sale.”,true);
$award_tier_commission = false;
}
}
if ($award_tier_commission)
{
if(!empty($wp_aff_affiliates_db->sec_tier_commissionlevel)){
$second_tier_commission_level = $wp_aff_affiliates_db->sec_tier_commissionlevel;
wp_affiliate_log_debug("Using the affiliate specific 2nd tier commission for this referral. 2nd tier commission level: ".$second_tier_commission_level,true);
}
else{
$second_tier_commission_level = get_option(‘wp_aff_2nd_tier_commission_level’);
wp_affiliate_log_debug("Using global 2nd tier commission for this referral. 2nd tier commission level: ".$second_tier_commission_level,true);
}
if (get_option(‘wp_aff_use_fixed_commission’))
{
$commission_amount = $second_tier_commission_level;
}
else
{
$commission_amount = round(($second_tier_commission_level * $sale_amount)/100,2);
}
$campaign_id = “”;
$is_tier_comm = “yes”;
global $wpdb;
$aff_sales_table = WP_AFF_SALES_TBL_NAME;
$updatedb = “INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id,buyer_name,is_tier_comm) VALUES (’$wp_aff_affiliates_db->referrer’,’$clientdate’,’$clienttime’,’’,’’,’$commission_amount’,’$sale_amount’,’$txn_id’,’$item_id’,’$buyer_email’,’$campaign_id’,’$buyer_name’,’$is_tier_comm’)”;
$results = $wpdb->query($updatedb);
$aff_tx_msg .= '
Tier commission awarded to: ‘.$wp_aff_affiliates_db->referrer.’. Commission amount: '.$commission_amount;
wp_affiliate_log_debug('Tier commission awarded to: ‘.$wp_aff_affiliates_db->referrer.’. Commission amount: '.$commission_amount,true);
}
}
return $aff_tx_msg;
}


//This function added by me for 3rd tier but not working

function wp_aff_award_third_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name=’’)
{
global $aff_tx_msg;
$clientdate = (date (“Y-m-d”));
$clienttime = (date (“H:i:s”));

if (get_option(‘wp_aff_use_3tier’) && !empty($wp_aff_affiliates_db->referrer))
{
$aff_tx_msg .= ‘
Using tier model’;
wp_affiliate_log_debug(“Using tier model”,true);
$award_tier_commission = true;
$duration = get_option(‘wp_aff_3rd_tier_duration’);
if(!empty($duration))
{
$join_date = $wp_aff_affiliates_db->date;
$days_since_joined = round((strtotime(date(“Y-m-d”)) - strtotime($join_date) ) / (60 * 60 * 24));

if ($days_since_joined > $duration)
{
$aff_tx_msg .= ‘
Tier commission award duration expried’;
wp_affiliate_log_debug(“Tier commission award duration expried! No tier commission will be awarded for this sale.”,true);
$award_tier_commission = false;
}
}
if ($award_tier_commission)
{
{
$third_tier_commission_level = get_option(‘wp_aff_3rd_tier_commission_level’);
wp_affiliate_log_debug("Using global 3rd tier commission for this referral. 3rd tier commission level: ".$third_tier_commission_level,true);
}
if (get_option(‘wp_aff_use_fixed_commission’))
{
$commission_amount = $third_tier_commission_level;
}
else
{
$commission_amount = round(($third_tier_commission_level * $sale_amount)/100,2);
}
$campaign_id = “”;
$is_tier_comm = “yes”;
global $wpdb;
$aff_sales_table = WP_AFF_SALES_TBL_NAME;
$updatedb = “INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id,buyer_name,is_tier_comm) VALUES (’$wp_aff_affiliates_db->referrer’,’$clientdate’,’$clienttime’,’’,’’,’$commission_amount’,’$sale_amount’,’$txn_id’,’$item_id’,’$buyer_email’,’$campaign_id’,’$buyer_name’,’$is_tier_comm’)”;
$results = $wpdb->query($updatedb);
$aff_tx_msg .= '
Tier commission awarded to: ‘.$wp_aff_affiliates_db->referrer.’. Commission amount: '.$commission_amount;
wp_affiliate_log_debug('Tier commission awarded to: ‘.$wp_aff_affiliates_db->referrer.’. Commission amount: '.$commission_amount,true);
}
}
return $aff_tx_msg;
}

First you will get better response to your question if you put your code in php tags (or code tags), for example:

[php]<?php
include(‘lib/includes/config.php’);
$e = array();
$query = ‘SELECT team, pollName, votes FROM votingbooth’;
$stmt = $pdo->prepare($query);
$stmt->execute();
$x = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$e[$x][‘team’] = $row[‘team’];
$e[$x][‘pollName’] = $row[‘pollName’];
$e[$x][‘votes’] = $row[‘votes’];
$x += 1;
}
print json_encode($e);[/php]

It makes it easier for the person to read your code and give a reply back.

Second, try only put the code that you think is causing the problem. If by chance you don’t post enough, the person helping you usually will ask for more code. People helping you not only look at your code, but the code that they are working on themselves and after awhile it can drive a person a little loopy. :-\ ;D

Thirdly and lastly, try to explain what is causing the problem in detail, saying it doesn’t work or using jargon that isn’t code related doesn’t help you solve the problem. Like I have no idea what you are talking about when you say “3rd tier commission”. A better way to explain the problem is telling us what error you are getting, for example “Undefined variable at line 99”. Or if you think it’s a logic error state in the code where you think it’s happening at. I hope I made myself clear, if not someone will probably explain what I am trying to get across. :wink:

Doing a quick scan of your code the way it is I see one major problem.
First you do this global $aff_tx_msg - a global variable which is a bad thing in itself.
Second, down further in the same function (I think) your return $aff_tx_msg, it is a global variable it doesn’t make much sense to return it. One of the main points in having a functions is trying to keep variables in scope as much as possible.

Hi

I need to modify the function wp_aff_award_second_tier_commission. currently it is working for 2 levels commission (2 tier) but i need it up to 7 levels (7 tier). please help me

[php]function wp_aff_award_commission($referrer,$sale_amount,$txn_id,$item_id,$buyer_email,$clientip=’’,$comm_rate=’’,$buyer_name=’’)
{
global $aff_tx_msg,$aff_error_msg;
$commission_award_result = “”;
$debug_data = “”;
$debug_data .= "Referrer: ".$referrer. ", Sale Amount: “.$sale_amount.”, Transaction ID: “.$txn_id.”, ";
$debug_data .= "Item ID: ".$item_id. ", Buyer Email: “.$buyer_email.”, Custom Commission Rate: ".$comm_rate;
$aff_tx_msg = $debug_data;
wp_affiliate_log_debug($debug_data,true);
$clientdate = (date (“Y-m-d”));
$clienttime = (date (“H:i:s”));
if(empty($clientip))
{
$clientip = $_SERVER[‘REMOTE_ADDR’];
}
if(empty($txn_id))
{
$txn_id = uniqid();
}
if (!empty($referrer))
{
global $wpdb;
$affiliates_table_name = $wpdb->prefix . “affiliates_tbl”;
$aff_sales_table = $wpdb->prefix . “affiliates_sales_tbl”;
$wp_aff_affiliates_db = $wpdb->get_row(“SELECT * FROM $affiliates_table_name WHERE refid = ‘$referrer’”, OBJECT);
if(empty($comm_rate)){
$commission_level = $wp_aff_affiliates_db->commissionlevel;
}
else{
$commission_level = $comm_rate;
}

	if (get_option('wp_aff_use_fixed_commission'))
	{
        $commission_amount = $commission_level;
    }
    else
    {
	    $commission_amount = ($commission_level * $sale_amount)/100;
    }
	    
	if(WP_AFFILIATE_NO_COMMISSION_FOR_SELF_PURCHASE == '1'){
		if(!empty($buyer_email)){
			if(wp_aff_check_if_buyer_is_referrer($referrer,$buyer_email)){
				wp_affiliate_log_debug('The buyer ('.$buyer_email.') is the referrer ('.$referrer.') so this sale is NOT ELIGIBLE for generating any commission.',true);
				return true;
			}
		}
		else{
			 wp_affiliate_log_debug("Buyer email data is missing from the request so the plugin cannot verify the WP_AFFILIATE_NO_COMMISSION_FOR_SELF_PURCHASE option",true);
		}
	}

	$commission_amount = round($commission_amount,2);
    $c_id='';
    if(isset($_COOKIE['c_id'])){
    	$c_id = $_COOKIE['c_id'];
    }
    
    if(!empty($buyer_name)){
		$updatedb = "INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id,buyer_name) VALUES ('$referrer','$clientdate','$clienttime','','$clientip','$commission_amount','$sale_amount','$txn_id','$item_id','$buyer_email','$c_id','$buyer_name')";        	
    }
    else{
   		$updatedb = "INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id) VALUES ('$referrer','$clientdate','$clienttime','','$clientip','$commission_amount','$sale_amount','$txn_id','$item_id','$buyer_email','$c_id')";
    }		
   	$results = $wpdb->query($updatedb);	
	if(!$results)
	{
		$aff_tx_msg .= "<br />The database update query failed for table: ".$aff_sales_table;
		wp_affiliate_log_debug("The database update query failed for table: ".$aff_sales_table,false);
	}		
	else
	{		
		$aff_tx_msg .= '<br />The sale has been registered in the WP Affiliate Platform Database for referrer: '.$referrer.' with amount: '.$commission_amount;
		wp_affiliate_log_debug('The sale has been registered in the WP Affiliate Platform Database for referrer: '.$referrer.' with amount: '.$commission_amount,true);
	}
	wp_aff_send_commission_notification($wp_aff_affiliates_db->email,$txn_id);
	
	// 2nd tier commission
	$commission_award_result = wp_aff_award_second_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name);
}
return $commission_award_result;		

}

function wp_aff_award_second_tier_commission($wp_aff_affiliates_db,$sale_amount,$txn_id,$item_id,$buyer_email,$buyer_name=’’)
{
global $aff_tx_msg;
$clientdate = (date (“Y-m-d”));
$clienttime = (date (“H:i:s”));

	if (get_option('wp_aff_use_2tier') && !empty($wp_aff_affiliates_db->referrer))
	{
		$aff_tx_msg .= '<br />Using tier model';
		wp_affiliate_log_debug("Using tier model",true);
		$award_tier_commission = true;	
		$duration = get_option('wp_aff_2nd_tier_duration');		
		if(!empty($duration))
		{
			$join_date = $wp_aff_affiliates_db->date;
			$days_since_joined = round((strtotime(date("Y-m-d")) - strtotime($join_date) ) / (60 * 60 * 24));
			
			if ($days_since_joined > $duration)
			{
				$aff_tx_msg .= '<br />Tier commission award duration expried';
				wp_affiliate_log_debug("Tier commission award duration expried! No tier commission will be awarded for this sale.",true);
				$award_tier_commission = false;
			}
		}				
		if ($award_tier_commission)
		{
			if(!empty($wp_aff_affiliates_db->sec_tier_commissionlevel)){
				$second_tier_commission_level = $wp_aff_affiliates_db->sec_tier_commissionlevel;
				wp_affiliate_log_debug("Using the affiliate specific 2nd tier commission for this referral. 2nd tier commission level: ".$second_tier_commission_level,true);
			}
			else{
				$second_tier_commission_level = get_option('wp_aff_2nd_tier_commission_level');
				wp_affiliate_log_debug("Using global 2nd tier commission for this referral. 2nd tier commission level: ".$second_tier_commission_level,true);
			}
			if (get_option('wp_aff_use_fixed_commission'))
			{
                $commission_amount = $second_tier_commission_level;
            }
            else
            {
			    $commission_amount = round(($second_tier_commission_level * $sale_amount)/100,2);
            }
            $campaign_id = "";
            $is_tier_comm = "yes";
			global $wpdb;
			$aff_sales_table = WP_AFF_SALES_TBL_NAME;
			$updatedb = "INSERT INTO $aff_sales_table (refid,date,time,browser,ipaddress,payment,sale_amount,txn_id,item_id,buyer_email,campaign_id,buyer_name,is_tier_comm) VALUES ('$wp_aff_affiliates_db->referrer','$clientdate','$clienttime','','','$commission_amount','$sale_amount','$txn_id','$item_id','$buyer_email','$campaign_id','$buyer_name','$is_tier_comm')";
			$results = $wpdb->query($updatedb);	
			$aff_tx_msg .= '<br />Tier commission awarded to: '.$wp_aff_affiliates_db->referrer.'. Commission amount: '.$commission_amount;
			wp_affiliate_log_debug('Tier commission awarded to: '.$wp_aff_affiliates_db->referrer.'. Commission amount: '.$commission_amount,true);	
		}			
	}	
	return $aff_tx_msg;

}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service