Advertisement
palsushobhan

Missing-vendor-commission-regenerate-daily-scheduler

Sep 3rd, 2024 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. function wcfm_check_and_regenerate_missing_commissions() {
  2.     $batch_size      = 20;  // Number of orders to process in each batch
  3.     $processed       = 0;   // Counter for processed orders
  4.     $total_processed = 0;   // Counter for total processed orders
  5.     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
  6.     do {
  7.         $args = [
  8.             'limit'      => $batch_size, // Process in batches of 20
  9.             'type'       => 'shop_order',
  10.             'meta_query' => [
  11.                 'relation' => 'AND',
  12.                 [
  13.                     'key'     => '_wcfmmp_order_processed',
  14.                     'compare' => 'NOT EXISTS', // Orders without '_wcfmmp_order_processed' meta key
  15.                 ],
  16.                 [
  17.                     'key'     => '_wcfmmp_not_valid_vendor_order',
  18.                     'compare' => 'NOT EXISTS', // Orders without '_another_meta_key' meta key
  19.                 ],
  20.             ],
  21.         ];
  22.         $orders    = wc_get_orders( $args );
  23.         $processed = count($orders);
  24.         foreach ( $orders as $order ) {
  25.             $order_id = $order->get_id();
  26.             do_action('wcfm_manual_order_reset', $order_id, true);
  27.             $order_posted = wc_get_order($order_id);
  28.             do_action('wcfm_manual_order_processed', $order_id, $order_posted, $order_posted);
  29.             $order->update_meta_data( '_wcfmmp_order_processed', 'yes' );
  30.             $order->save(); // Save the changes
  31.             wcfm_create_log('Processed order: #' . $order_id, 'info', 'wcfm-commission');
  32.         }
  33.         $total_processed += $processed;
  34.         if ($processed > 0) {
  35.             sleep(2);
  36.         }
  37.     } while ( $processed === $batch_size );
  38.  
  39.     wcfm_create_log('Total orders processed: ' . $total_processed, 'info', 'wcfm-commission');
  40. }
  41.  
  42. function run_daily_missing_commission_generate() {
  43.     if ( ! as_next_scheduled_action( 'wcfm_check_and_regenerate_missing_commissions' ) ) {
  44.         WC()->queue()->schedule_recurring( time(), DAY_IN_SECONDS, 'wcfm_check_and_regenerate_missing_commissions' );
  45.     }
  46. }
  47. add_action( 'init', 'run_daily_missing_commission_generate' );
  48. add_action( 'wcfm_check_and_regenerate_missing_commissions', 'wcfm_check_and_regenerate_missing_commissions' );
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement