Last active
June 15, 2024 20:22
-
-
Save goranefbl/1d64f80f8b3909aad4763d690d694b58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Function to get the referrer user ID | |
function get_referrer_user_id($rafID) { | |
// Check if the referrer is a guest using email | |
if (filter_var($rafID, FILTER_VALIDATE_EMAIL)) { | |
return $rafID; | |
} | |
// Retrieve the referrer user ID based on the custom meta field 'gens_referral_id' | |
$gens_users = get_users(array( | |
"meta_key" => "gens_referral_id", | |
"meta_value" => $rafID, | |
"number" => 1, | |
"fields" => "ID" | |
)); | |
// Return the user ID if found, otherwise return false | |
if (is_array($gens_users) && !empty($gens_users)) { | |
return $gens_users[0]; | |
} else { | |
return false; | |
} | |
} | |
// Function to get the subscription level of a user | |
function get_user_subscription_level($user_id) { | |
// Return 'none' if no user ID is provided | |
if (!$user_id) { | |
return 'none'; | |
} | |
// Get the user's subscriptions | |
$subscriptions = wcs_get_users_subscriptions($user_id); | |
foreach ($subscriptions as $subscription) { | |
// Check if the subscription is active or on hold | |
if ($subscription->has_status(array('active', 'on-hold'))) { | |
// Get the product ID from the subscription | |
$product_id = $subscription->get_items()[0]->get_product_id(); | |
// Return the subscription level based on the product ID | |
if ($product_id == 'basic_product_id') { | |
return 'basic'; | |
} elseif ($product_id == 'middle_product_id') { | |
return 'middle'; | |
} elseif ($product_id == 'advanced_product_id') { | |
return 'advanced'; | |
} | |
} | |
} | |
// Return 'none' if no active subscriptions are found | |
return 'none'; | |
} | |
// Function to get the referral discount based on the subscription level | |
function get_referral_discount($subscription_level) { | |
switch ($subscription_level) { | |
case 'basic': | |
return 5; // 5% discount for basic level | |
case 'middle': | |
return 10; // 10% discount for middle level | |
case 'advanced': | |
return 15; // 15% discount for advanced level | |
default: | |
return 0; // No discount | |
} | |
} | |
// Hook into the WooCommerce filter to apply the referral discount | |
add_filter('prefix_coupon_amount', 'apply_referral_discount', 10, 2); | |
function apply_referral_discount($amount, $order_id) { | |
// Get the order | |
$order = wc_get_order($order_id); | |
// Get the referrer ID from the order meta | |
$rafID = esc_attr($order->get_meta('_raf_id', true)); | |
// Get the referrer user ID using the function defined earlier | |
$referrer_user_id = get_referrer_user_id($rafID); | |
// Get the subscription level of the referrer | |
$subscription_level = get_user_subscription_level($referrer_user_id); | |
// Get the discount amount based on the subscription level | |
$discount = get_referral_discount($subscription_level); | |
// Apply the discount to the amount | |
$amount = $amount - ($amount * ($discount / 100)); | |
return $amount; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment