woocommerce_helper = $woocommerce_helper; $this->woocommerce_purchases = $woocommerce_purchases; parent::__construct($timer); } function checkProcessingRequirements() { return $this->woocommerce_helper->isWooCommerceActive() && empty(self::getCompletedTasks()); // run only once } function processTaskStrategy(ScheduledTask $task) { $oldest_click = StatisticsClicks::orderByAsc('created_at')->limit(1)->findOne(); if (!$oldest_click) { return true; } // continue from 'last_id' processed by previous run $meta = $task->getMeta(); $last_id = isset($meta['last_id']) ? $meta['last_id'] : 0; add_filter('posts_where', function ($where = '') use ($last_id) { return $where . ' AND wp_posts.ID > ' . $last_id; }, 10, 2); $order_ids = $this->woocommerce_helper->wcGetOrders([ 'status' => 'completed', 'date_completed' => '>=' . $oldest_click->created_at, 'orderby' => 'ID', 'order' => 'ASC', 'limit' => self::BATCH_SIZE, 'return' => 'ids', ]); if (empty($order_ids)) { return true; } foreach ($order_ids as $order_id) { $this->woocommerce_purchases->trackPurchase($order_id, false); } $task->meta = ['last_id' => end($order_ids)]; $task->save(); return false; } static function getNextRunDate() { return Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp')); // schedule immediately } }