Untitled

 avatar
unknown
plain_text
a month ago
1.5 kB
6
Indexable
add_filter('b2bking_group_rules_count_last_order','__return_true');
add_filter('b2bking_total_order_amount_used_group_rules', function($total, $user_id) {
    // Get the start of current year
    $year_start = date('Y-01-01');
    
    // Get all customer orders
    $orders = wc_get_orders(array(
        'customer_id' => $user_id,
        'date_created' => '>=' . $year_start,
        'status' => array('wc-completed', 'wc-processing', 'wc-on-hold'), 
        'limit' => -1 // Get all orders
    ));
    
    // Calculate total
    $year_total = 0;
    foreach ($orders as $order) {
        $year_total += $order->get_total();
    }
    
    return $year_total;
}, 10, 2);

add_action('yearly_customer_group_reset', function() {
    $base_group = 1234; // replace this with the basic (level 1) customer group

    $users = get_users(array(
        'fields'=> array('ID', 'user_login'),
    ));
    
    // move agents from new group to old group
    foreach ($users as $user) {
        update_user_meta($user->ID, 'b2bking_customergroup', $base_group);
    }
    
});
add_filter('cron_schedules', function($schedules) {
    $schedules['yearly'] = array(
        'interval' => 31536000, // 365 days in seconds
        'display'  => __('Once Yearly')
    );
    return $schedules;
});
add_action('init', function() {
    if (!wp_next_scheduled('yearly_customer_group_reset')) {
        wp_schedule_event(strtotime('next year January 1st midnight'), 'yearly', 'yearly_customer_group_reset');
    }
});
Editor is loading...
Leave a Comment