Untitled

 avatar
webwizards
plain_text
a year ago
3.1 kB
13
Indexable
/**
 * B2BKing Anniversary-Based Group Changes
 * 
 * Changes customer groups on their registration anniversary if they haven't 
 * made any purchases in the last 12 months (excluding signup year).
 * 
 */

function b2bking_check_group_changes() {
    // Configuration
    $active_group_id = 123;      // Current/active customer group ID
    $inactive_group_id = 456;    // Inactive customer group ID (for customers with no purchases)
    
    // Get today's date
    $today = date('m-d');
    
    // Prevent running multiple times on the same day
    $last_run = get_option('b2bking_last_anniversary_check', '');
    $today_full = date('Y-m-d');
    
    if ($last_run === $today_full) {
        return;
    }
    
    // Get all customers who registered on this day in previous years
    $customers = get_users([
        'role' => 'customer',
        'fields' => ['ID', 'user_registered'],
        'meta_query' => [
            [
                'key' => 'user_registered',
                'compare' => 'EXISTS'
            ]
        ]
    ]);
    
    foreach ($customers as $customer) {
        $registration_date = date('m-d', strtotime($customer->user_registered));
        $registration_year = date('Y', strtotime($customer->user_registered));
        $current_year = date('Y');
        
        // Check if today is their registration anniversary and they've been registered for at least 1 full year
        if ($registration_date === $today && $current_year > $registration_year) {
            
            // Get their purchases in the last 12 months
            $has_purchases = b2bking_customer_has_purchases_last_12_months($customer->ID);
            
            // Get current group
            $current_group = b2bking()->get_user_group($customer->ID);
            
            if ($has_purchases) {
                // Customer has purchases - ensure they're in active group
                if ($current_group != $active_group_id) {
                    b2bking()->update_user_group($customer->ID, $active_group_id);
                }
            } else {
                // Customer has no purchases in last 12 months - move to inactive group
                if ($current_group != $inactive_group_id) {
                    b2bking()->update_user_group($customer->ID, $inactive_group_id);
                }
            }
        }
    }
    
    // Mark today as processed
    update_option('b2bking_last_anniversary_check', $today_full);
}
add_action('wp_loaded', 'b2bking_check_group_changes');

function b2bking_customer_has_purchases_last_12_months($customer_id) {
    // Get date 12 months ago
    $twelve_months_ago = date('Y-m-d H:i:s', strtotime('-12 months'));
    $now = date('Y-m-d H:i:s');
    
    // Query for completed orders in the last 12 months
    $orders = wc_get_orders([
        'customer_id' => $customer_id,
        'status' => 'wc-completed',
        'date_created' => $twelve_months_ago . '...' . $now,
        'limit' => 1, // We only need to know if at least 1 order exists
        'return' => 'ids'
    ]);
    
    return !empty($orders);
}
Editor is loading...
Leave a Comment