// ============================================================
// Example 1: Minimum Order Count Requirement
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
// Require at least 3 completed orders
return function_exists('wc_get_customer_order_count')
&& wc_get_customer_order_count($user_id) >= 3;
}, 20, 2);
// ============================================================
// Example 2: Active Subscription Required
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
// Check if user has an active subscription (requires WooCommerce Subscriptions)
if (!function_exists('wcs_user_has_subscription')) {
return $is_allowed;
}
return wcs_user_has_subscription($user_id, '', 'active');
}, 20, 2);
// ============================================================
// Example 3: Minimum Total Spent
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
if (!class_exists('WC_Customer')) {
return $is_allowed;
}
$customer = new WC_Customer($user_id);
// Require at least $100 spent
return $customer->get_total_spent() >= 100;
}, 20, 2);
// ============================================================
// Example 4: Email Domain Restriction
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
$user = get_userdata($user_id);
if (!$user) {
return false;
}
// Only allow specific email domains
$allowed_domains = ['company.com', 'partner.com'];
$email_domain = substr(strrchr($user->user_email, '@'), 1);
return in_array($email_domain, $allowed_domains);
}, 20, 2);
// ============================================================
// Example 5: Multiple Conditions (Orders + Subscription)
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
// Require BOTH 5+ orders AND active subscription
$has_orders = function_exists('wc_get_customer_order_count')
&& wc_get_customer_order_count($user_id) >= 5;
$has_subscription = function_exists('wcs_user_has_subscription')
&& wcs_user_has_subscription($user_id, '', 'active');
return $has_orders && $has_subscription;
}, 20, 2);
// ============================================================
// Example 6: Membership Plugin Integration
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
// Example with WooCommerce Memberships plugin
if (!function_exists('wc_memberships_is_user_active_member')) {
return $is_allowed;
}
// Only allow members of "Gold" or "Platinum" plans
$allowed_plans = ['gold-membership', 'platinum-membership'];
foreach ($allowed_plans as $plan_slug) {
if (wc_memberships_is_user_active_member($user_id, $plan_slug)) {
return true;
}
}
return false;
}, 20, 2);
// ============================================================
// Example 7: Account Age Requirement
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
$user = get_userdata($user_id);
if (!$user) {
return false;
}
// Account must be at least 30 days old
$registration_date = strtotime($user->user_registered);
$days_since_registration = (time() - $registration_date) / DAY_IN_SECONDS;
return $days_since_registration >= 30;
}, 20, 2);
// ============================================================
// Example 8: Time-Based Restriction (Black Friday Only)
// ============================================================
add_filter('wpgens_loyalty_user_can_participate', function($is_allowed, $user_id) {
if (!$is_allowed) return false;
// Define your sale period
$sale_start = strtotime('2025-11-24'); // Black Friday 2025
$sale_end = strtotime('2025-11-30'); // Cyber Monday 2025
$current_time = current_time('timestamp');
// Only allow during sale period
return $current_time >= $sale_start && $current_time <= $sale_end;
}, 20, 2);