Affiliate module: fix referral approval lifecycle and settings reads
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -64,8 +64,32 @@ class AffiliateSettings {
|
||||
'description' => __('Allow affiliates to earn commission when their own user account places an order.', 'woonoow'),
|
||||
'default' => false,
|
||||
],
|
||||
'woonoow_affiliate_share_customer_data' => [
|
||||
'type' => 'toggle',
|
||||
'label' => __('Share Customer Data with Affiliates', 'woonoow'),
|
||||
'description' => __('Allow affiliates to see the name and email of the customers they refer.', 'woonoow'),
|
||||
'default' => false,
|
||||
],
|
||||
];
|
||||
|
||||
return $schemas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Affiliate module setting from module settings storage with legacy fallback.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get_setting($key, $default = null)
|
||||
{
|
||||
$module_settings = get_option('woonoow_module_affiliate_settings', []);
|
||||
if (is_array($module_settings) && array_key_exists($key, $module_settings)) {
|
||||
return $module_settings[$key];
|
||||
}
|
||||
|
||||
// Legacy fallback for older installs that may store direct option keys.
|
||||
return get_option($key, $default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,7 +363,7 @@ class AffiliateTracker
|
||||
|
||||
// Schedule auto-approval (e.g., 14 days) via Action Scheduler
|
||||
if (function_exists('as_schedule_single_action')) {
|
||||
$approval_days = get_option('woonoow_affiliate_holding_period', 14);
|
||||
$approval_days = (int) AffiliateSettings::get_setting('woonoow_affiliate_holding_period', 14);
|
||||
$timestamp = time() + ($approval_days * DAY_IN_SECONDS);
|
||||
as_schedule_single_action($timestamp, 'woonoow_approve_referral', ['referral_id' => $referral_id], 'woonoow_affiliate');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user