Affiliate module: fix referral approval lifecycle and settings reads

This commit is contained in:
Dwindi Ramadhana
2026-06-02 00:37:20 +07:00
parent f3c4ee7124
commit fec786daa6
8 changed files with 344 additions and 36 deletions

View File

@@ -107,11 +107,13 @@ class AffiliateLifecycle
if (!$referral) return;
// Check if holding period is 0 (immediate approval on completion)
$holding_period = (int) get_option('woonoow_affiliate_holding_period', 14);
$holding_period = (int) AffiliateSettings::get_setting('woonoow_affiliate_holding_period', 14);
$handled_now = false;
if ($holding_period === 0) {
// Immediate approval
self::auto_approve_referral($referral->id);
$handled_now = true;
} else {
// If order was completed BEFORE the scheduled action time, approve now
// Otherwise, the scheduled action will approve later
@@ -120,12 +122,13 @@ class AffiliateLifecycle
if (time() >= $approval_time) {
self::auto_approve_referral($referral->id);
$handled_now = true;
}
// If not, the scheduled Action Scheduler job will handle it
}
// Cancel the scheduled auto-approval since we're handling it now
if (function_exists('as_unschedule_all_actions')) {
// Only unschedule if we actually approved now.
if ($handled_now && function_exists('as_unschedule_all_actions')) {
as_unschedule_all_actions('woonoow_approve_referral', ['referral_id' => $referral->id], 'woonoow_affiliate');
}
}
@@ -222,13 +225,23 @@ class AffiliateLifecycle
if (!$referral) return; // Already processed or deleted
// Double check order status
// Double check order status.
// Referrals must never be approved before the order is completed.
$order = wc_get_order($referral->order_id);
if (!$order || in_array($order->get_status(), ['refunded', 'cancelled', 'failed'])) {
if (!$order) {
return;
}
$order_status = $order->get_status();
if (in_array($order_status, ['refunded', 'cancelled', 'failed'])) {
self::handle_order_cancelled($referral->order_id);
return;
}
if ($order_status !== 'completed') {
return;
}
// Approve referral
$wpdb->update(
$referrals_table,