Adding Affiliates info in your Contact Form 7 emails

Contact Form 7 + Affiliates = Power Tool 🙂
Affiliates plugin integrate the option to includo form field data in the referral email, but sometimes we need to add the affiliate’s info in the Contact Form 7 email notification.
Here a code that you can use to add affiliate’s information in your emails:


function addAffiliatesTokens($array) {

	if (! class_exists ( "Affiliates_Service" )) {
		include_once (AFFILIATES_CORE_LIB . '/class-affiliates-service.php');
	}
	$affiliate_id = Affiliates_Service::get_referrer_id ();

	if ($affiliate_id) {
		if ($affiliate_id !== affiliates_get_direct_id ()) {
			if ($user_id = affiliates_get_affiliate_user ( $affiliate_id )) {
				if ($user = get_user_by ( 'id', $user_id )) {
					$array['body'] = str_replace('[affiliate-id]', $affiliate_id, $array['body']);
					$array['body'] = str_replace('[affiliate-username]', $user->user_login, $array['body']);
				}
			}
		}
	}
	return $array;
}
add_filter('wpcf7_mail_components', 'addAffiliatesTokens');

It adds the tokens [affiliate-id] and [affiliate-username]