Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created December 29, 2025 13:26
Show Gist options
  • Select an option

  • Save goranefbl/6f1a0887fc370ce80111a7408d27fdc1 to your computer and use it in GitHub Desktop.

Select an option

Save goranefbl/6f1a0887fc370ce80111a7408d27fdc1 to your computer and use it in GitHub Desktop.
Check if customer bought product
/**
* Hide referral link/code if user hasn't purchased a specific product
*
* @param bool $hide Current hide status
* @param int $user_id Current user ID
* @return bool Whether to hide the referral link
*/
add_filter('gens_raf_hide_referral_link', function($hide, $user_id) {
// If already hidden, keep it hidden
if ($hide) {
return true;
}
// Product ID(s) that must be purchased to show referral link
// Change this to your product ID or array of product IDs
$required_product_ids = array(123); // Replace 123 with your product ID
// Check if user has purchased any of the required products
if ($user_id && function_exists('wc_customer_bought_product')) {
$user = get_user_by('id', $user_id);
if ($user) {
foreach ($required_product_ids as $product_id) {
if (wc_customer_bought_product($user->user_email, $user_id, $product_id)) {
return false; // User bought the product, show referral link
}
}
}
}
// User hasn't purchased the required product, hide referral link
return true;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment