Primary

Example

Implementing a custom referral amount method

Method signature

A custom method has to accept two arguments, the affiliate ID and an array of parameters:

/**
 * Custom referral amount method.
 * @param int $affiliate_id the referring affiliate's ID
 * @param array $parameters referral data
 * @return string referral amount for the given affiliate
 */
my_custom_method( $affiliate_id, $parameters ) {
    $result = "0";
    ... // your custom calculation goes here
    return $result;
}

$parameters provides the following information:

  • $parameters['affiliate_ids'] array of int containing the affiliate IDs of all affiliates that are credited with a referral for the same transaction, this will contain only the $affiliate_id when one affiliate is to be credited, otherwise it will also contain the IDs of the other affiliates that should be credited for the same transaction
  • $parameters['post_id'] int post ID (in some cases this holds the transaction or order ID)
  • $parameters['description'] string referral description
  • $parameters['data'] array referral data
  • $parameters['base_amount'] string net transaction amount that the referral’s amount is to be based on
  • $parameters['amount'] string fixed referral amount
  • $parameters['currency_id'] string three letter currency code
  • $parameters['status'] string referral status
  • $parameters['type'] string referral type
  • $parameters['reference'] string transaction ID
  • $parameters['test'] boolean if true no referral is recorded

Registering a method

The custom method must be registered by calling:

Affiliates_Referral::register_referral_amount_method( 'my_custom_method' )

It will then be available when Referral Amount Method is chosen as the Default referral calculation on the Affiliates > Settings admin page. It can also be specified for individual affiliates as an attribute.

Example Plugin

Download this plugin

/**
 * Plugin Name: Affiliates Custom Method
 * Description: Implements an example method for use with Affiliates Pro.
 * Version: 1.0.1
 * Author: itthinx
 * Author URI: http://www.itthinx.com
 */
class ACM {

	/**
	 * Registers a custom referral amount method.
	 */
	public static function init() {
		if ( class_exists( 'Affiliates_Referral' ) ) {
			Affiliates_Referral::register_referral_amount_method( array( __CLASS__, 'custom_method' ) );
		}
	}

	/**
	 * Custom referral amount method implementation.
	 * @param int $affiliate_id
	 * @param array $parameters
	 */
	public static function custom_method( $affiliate_id = null, $parameters = null ) {
		$result = "0";
		if ( isset( $parameters['base_amount'] ) ) {
			$result = bcmul( "0.25", $parameters['base_amount'], 2 );
		}
		return $result;
	}
}
add_action( 'init', array( 'ACM', 'init' ) );