Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Last active February 26, 2025 11:02
Show Gist options
  • Save goranefbl/3f77f1a37c7f8389a85760d7a31850ef to your computer and use it in GitHub Desktop.
Save goranefbl/3f77f1a37c7f8389a85760d7a31850ef to your computer and use it in GitHub Desktop.
Hide referral link for users that spent below 300
<?php
add_filter('wpgens_raf_code', 'gens_raf_code', 10, 1);
function gens_raf_code($raf_code) {
$current_user = wp_get_current_user();
$total_spent = get_user_total_spent($current_user->ID);
if ($total_spent < 300) {
return 'Referral code is available only to users who have spent at least 300';
}
return $raf_code;
}
add_filter('wpgens_raf_link', 'gens_raf_link', 10, 3);
function gens_raf_link($raf_link, $referral_id, $type) {
$current_user = wp_get_current_user();
$total_spent = get_user_total_spent($current_user->ID);
if ($total_spent < 300) {
return 'Referral link is available only to users who have spent at least 300';
}
return $raf_link;
}
// Helper function to get the total amount spent by a user
function get_user_total_spent($user_id) {
$customer = new WC_Customer($user_id);
return $customer->get_total_spent();
}
add_filter('body_class', 'add_spent_based_body_class');
function add_spent_based_body_class($classes) {
$current_user = wp_get_current_user();
if ($current_user->ID) {
$total_spent = get_user_total_spent($current_user->ID);
if ($total_spent < 300) {
$classes[] = 'hide-referral-elements';
}
}
return $classes;
}
add_action('wp_footer', 'add_spent_based_css');
function add_spent_based_css() {
$current_user = wp_get_current_user();
if ($current_user->ID) {
$total_spent = get_user_total_spent($current_user->ID);
if ($total_spent < 300) {
echo '<style>.hide-referral-elements .gens-referral_coupons__table,.hide-referral-elements .gens-referral_coupons__title, .hide-referral-elements .gens-referral_coupons__title, .hide-referral-elements .gens-raf-field--social, .hide-referral-elements .gens-raf-field--email { display: none; }</style>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment