Untitled

mail@pastecode.io avatar
unknown
plain_text
8 days ago
2.0 kB
10
Indexable
// Register the cron schedule if it doesn't exist
if (!wp_next_scheduled('check_vendor_monthly_sales')) {
    wp_schedule_event(strtotime('first day of next month midnight'), 'monthly', 'check_vendor_monthly_sales');
}

// Hook for the monthly check
add_action('check_vendor_monthly_sales', 'check_all_vendors_monthly_sales');

function check_all_vendors_monthly_sales() {
    // Get all vendors
    $vendors = marketking()->get_all_vendors();
    
    if (empty($vendors) || !is_array($vendors)) {
        return;
    }
    
    // Get the date range for the past month
    $date_end = new DateTime('midnight first day of this month');
    $date_start = clone $date_end;
    $date_start->modify('-1 month');
    
    foreach ($vendors as $vendor) {
        if (!is_a($vendor, 'WP_User')) {
            continue;
        }
        
        $vendor_id = $vendor->ID;
        
        // Set up the order query arguments
        $args = array(
            'limit'        => -1,
            'type'         => 'shop_order',
            'status'       => array('wc-completed', 'wc-processing'),
            'meta_key'     => '_post_author',
            'meta_value'   => $vendor_id,
            'meta_compare' => '=',
            'date_query'   => array(
                'after'  => $date_start->format('Y-m-d'),
                'before' => $date_end->format('Y-m-d'),
                'inclusive' => true
            )
        );
        
        // Get the orders
        $orders = wc_get_orders($args);
        
        // Calculate total sales
        $total_sales = 0;
        foreach ($orders as $order) {
            if (!is_a($order, 'WC_Order')) {
                continue;
            }
            $total_sales += $order->get_total() - $order->get_total_refunded();
        }
        
        // Check if sales are below threshold
        if ($total_sales < 300) {
            update_user_meta($vendor->ID, 'marketking_group', 12345);
        }
    }
}
Leave a Comment