Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mholubowski/510333c8632394f35dde4133a9624a10 to your computer and use it in GitHub Desktop.
Save mholubowski/510333c8632394f35dde4133a9624a10 to your computer and use it in GitHub Desktop.
solid-affiliate-snippet-get_user_id_of_affiliate_who_referred_the_current_visitor.php
<?php
/**
* This function will attempt to find the Affiliate who referred the current visitor,
* and then return the user id of that affiliate.
*
* Example:
* $id = get_user_id_sld (); defaults user id to "1"
* $id = get_user_id_sld ( 2 ); defaults user id to "2"
*
* @param integer $user_id_to_return_if_no_affiliate_found
* @return integer
*/
function get_user_id_of_affiliate_who_referred_the_current_visitor($user_id_to_return_if_no_affiliate_found = 1)
{
// Test to see if SolidAffiliate is installed
if (class_exists(\SolidAffiliate\Main::class)) {
// Check to see if there is an Affiliate for the current page load.
$affiliate = \SolidAffiliate\Lib\VisitTracking::_affiliate_from_request($_REQUEST);
if (!is_null($affiliate)) {
return $affiliate->user_id;
}
// If not, Check if cookie is already set.
$visit_id_or_false = \SolidAffiliate\Lib\VisitTracking::get_cookied_visit_id();
if ($visit_id_or_false) {
$visit = \SolidAffiliate\Models\Visit::find($visit_id_or_false);
if (!is_null($visit)) {
$affiliate = \SolidAffiliate\Models\Affiliate::find($visit->affiliate_id);
if (!is_null($affiliate)) {
return $affiliate->user_id;
}
}
}
}
return $user_id_to_return_if_no_affiliate_found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment