Untitled
unknown
plain_text
12 days ago
6.4 kB
5
Indexable
// Configure B2BKing groups // EDIT this to match your groups and thresholds function b2bking_init_auto_groups() { $config = [ 'groups' => [ 'basic' => [ 'id' => 1, // Replace with actual Basic group ID 'threshold' => 0, 'name' => 'Basic' ], 'standard' => [ 'id' => 2, // Replace with actual Standard group ID 'threshold' => 200, 'name' => 'Standard' ], 'plus' => [ 'id' => 3, // Replace with actual Plus group ID 'threshold' => 1000, 'name' => 'Plus' ], 'premium' => [ 'id' => 4, // Replace with actual Premium group ID 'threshold' => 3000, 'name' => 'Premium' ] ], 'minimum_order_for_standard' => 200 // Minimum order amount to move from Basic to Standard ]; update_option('b2bking_groups_config', $config); } add_action('init', 'b2bking_init_auto_groups', 5); add_action('wp_loaded', 'b2bking_check_monthly_updates'); add_action('woocommerce_order_status_completed', 'b2bking_check_basic_to_standard_transition', 10, 1); /** * Check if today is the 1st of the month and run monthly group updates if needed */ function b2bking_check_monthly_updates() { $last_run_date = get_option('b2bking_last_monthly_update', ''); $today = date('Y-m-d'); $is_first_day_of_month = (date('j') == 1); // Run if today is the 1st of the month and we haven't run yet today if ($is_first_day_of_month && $last_run_date != $today) { // Get all customers $customer_query = new WP_User_Query([ 'role__in' => ['customer'], 'fields' => 'ID' ]); $customer_ids = $customer_query->get_results(); foreach ($customer_ids as $customer_id) { b2bking_update_customer_group_based_on_monthly_spend($customer_id); } // Save today's date as last run date update_option('b2bking_last_monthly_update', $today); } } /** * Update a customer's group based on their previous month's spend */ function b2bking_update_customer_group_based_on_monthly_spend($customer_id) { $config = get_option('b2bking_groups_config', []); $current_group_id = b2bking()->get_user_group($customer_id); // If user is in Basic group, skip (Basic to Standard transition is handled separately) if ($current_group_id == $config['groups']['basic']['id']) { return; } $previous_month_spend = b2bking_get_customer_completed_orders_total($customer_id, 'previous'); $new_group_id = b2bking_determine_group_by_spend($previous_month_spend); // Special case: Don't downgrade below Standard if ($new_group_id == $config['groups']['basic']['id']) { $new_group_id = $config['groups']['standard']['id']; } // Update group if needed if ($current_group_id != $new_group_id) { b2bking()->update_user_group($customer_id, $new_group_id); } } /** * Check if a Basic user should be upgraded to Standard on order completion */ function b2bking_check_basic_to_standard_transition($order_id) { $config = get_option('b2bking_groups_config', []); $order = wc_get_order($order_id); $customer_id = $order->get_customer_id(); // If no customer ID (guest checkout), skip if (!$customer_id) { return; } $current_group_id = b2bking()->get_user_group($customer_id); // Only proceed if user is in Basic group if ($current_group_id != $config['groups']['basic']['id']) { return; } // Check if this order meets the minimum threshold if ($order->get_total() >= $config['minimum_order_for_standard']) { // Move directly to Standard or higher based on current month's spend $monthly_spend = b2bking_get_customer_completed_orders_total($customer_id, 'current'); $new_group_id = b2bking_determine_group_by_spend($monthly_spend); b2bking()->update_user_group($customer_id, $new_group_id); } } /** * Determine appropriate group based on spend amount */ function b2bking_determine_group_by_spend($spend) { $config = get_option('b2bking_groups_config', []); // Default to basic group $appropriate_group_id = $config['groups']['basic']['id']; // Check against thresholds from highest to lowest if ($spend >= $config['groups']['premium']['threshold']) { $appropriate_group_id = $config['groups']['premium']['id']; } elseif ($spend >= $config['groups']['plus']['threshold']) { $appropriate_group_id = $config['groups']['plus']['id']; } elseif ($spend >= $config['groups']['standard']['threshold']) { $appropriate_group_id = $config['groups']['standard']['id']; } return $appropriate_group_id; } function b2bking_get_customer_completed_orders_total($customer_id, $period = 'current') { // Define date parameters based on period $now = current_time('timestamp'); // Set up the query arguments with common parameters $args = array( 'customer_id' => $customer_id, 'status' => array('wc-completed'), 'return' => 'ids', 'limit' => -1 ); if ($period === 'current') { // Current month: from 1st of current month to now $start_date = date('Y-m-01 00:00:00', $now); $end_date = date('Y-m-d H:i:s', $now); $args['date_created'] = $start_date . '...' . $end_date; } elseif ($period === 'previous') { // Previous month: from 1st to last day of previous month $first_day_previous_month = date('Y-m-01 00:00:00', strtotime('-1 month', $now)); $last_day_previous_month = date('Y-m-t 23:59:59', strtotime('-1 month', $now)); $args['date_created'] = $first_day_previous_month . '...' . $last_day_previous_month; } // For 'total' period, we don't add date constraints, so it will get all completed orders // Get the orders $orders = wc_get_orders($args); // Calculate the total $total = 0; foreach ($orders as $order_id) { $order = wc_get_order($order_id); $total += $order->get_total(); } return $total; }
Editor is loading...
Leave a Comment