Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wcfm_check_and_regenerate_missing_commissions() {
- $batch_size = 20; // Number of orders to process in each batch
- $processed = 0; // Counter for processed orders
- $total_processed = 0; // Counter for total processed orders
- wcfm_create_log("PERIODIC MISSING COMMISSION FINDER SCHEDULER START :: " . date_i18n(wc_date_format() . ' ' . wc_time_format(), current_time('timestamp', 0)), 'info', 'wcfm-commission'); // Start Log
- do {
- $args = [
- 'limit' => $batch_size, // Process in batches of 20
- 'type' => 'shop_order',
- 'meta_query' => [
- 'relation' => 'AND',
- [
- 'key' => '_wcfmmp_order_processed',
- 'compare' => 'NOT EXISTS', // Orders without '_wcfmmp_order_processed' meta key
- ],
- [
- 'key' => '_wcfmmp_not_valid_vendor_order',
- 'compare' => 'NOT EXISTS', // Orders without '_another_meta_key' meta key
- ],
- ],
- ];
- $orders = wc_get_orders( $args );
- $processed = count($orders);
- foreach ( $orders as $order ) {
- $order_id = $order->get_id();
- do_action('wcfm_manual_order_reset', $order_id, true);
- $order_posted = wc_get_order($order_id);
- do_action('wcfm_manual_order_processed', $order_id, $order_posted, $order_posted);
- $order->update_meta_data( '_wcfmmp_order_processed', 'yes' );
- $order->save(); // Save the changes
- wcfm_create_log('Processed order: #' . $order_id, 'info', 'wcfm-commission');
- }
- $total_processed += $processed;
- if ($processed > 0) {
- sleep(2);
- }
- } while ( $processed === $batch_size );
- wcfm_create_log('Total orders processed: ' . $total_processed, 'info', 'wcfm-commission');
- }
- function run_daily_missing_commission_generate() {
- if ( ! as_next_scheduled_action( 'wcfm_check_and_regenerate_missing_commissions' ) ) {
- WC()->queue()->schedule_recurring( time(), DAY_IN_SECONDS, 'wcfm_check_and_regenerate_missing_commissions' );
- }
- }
- add_action( 'init', 'run_daily_missing_commission_generate' );
- add_action( 'wcfm_check_and_regenerate_missing_commissions', 'wcfm_check_and_regenerate_missing_commissions' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement