/** * Order importer for WooCommerce. Process 10 orders at a time */ public static function wc_order_import() { $wpdb = Follow_Up_Emails::instance()->wpdb; if (empty($_POST['cmd'])) { self::send_response(array('error' => 'CMD is missing')); } $cmd = $_POST['cmd']; $email_id = !empty($_POST['email_id']) ? $_POST['email_id'] : ''; if ($email_id) { $fue_wc = Follow_Up_Emails::instance()->fue_wc; if ($cmd == 'start') { if (is_array($email_id)) { $total_orders = 0; foreach ($email_id as $id) { $email = new FUE_Email($id); $email_orders = $fue_wc->count_orders_for_email($email); $total_orders += $email_orders; if ($email_orders == 0) { delete_post_meta($id, '_import_order_flag'); } } self::send_response(array('total_orders' => $total_orders)); } else { $email = new FUE_Email($email_id); $total_orders = $fue_wc->count_orders_for_email($email); if ($total_orders == 0) { delete_post_meta($email_id, '_import_order_flag'); } self::send_response(array('total_orders' => $total_orders)); } } else { $email = new FUE_Email($email_id); $results = $fue_wc->import_orders_for_email($email, 10); if ($results['status'] == 'completed') { delete_post_meta($email_id, '_import_order_flag'); } self::send_response(array('status' => $results['status'] == 'running' ? 'partial' : 'completed', 'import_data' => $results['imported'], 'remaining_orders' => $results['remaining_orders'])); } } else { if ($cmd == 'start') { $tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}followup_order_items'"); if (empty($tables)) { self::send_response(array('error' => 'Database tables are not installed. Please deactivate then reactivate Follow Up Emails')); } if (!get_option('fue_orders_imported', false) && !get_transient('fue_importing_orders')) { // First run of the import script. Clear existing data for a fresh start $wpdb->query("DELETE FROM {$wpdb->prefix}followup_order_items"); $wpdb->query("DELETE FROM {$wpdb->prefix}followup_customers"); $wpdb->query("DELETE FROM {$wpdb->prefix}followup_order_categories"); $wpdb->query("DELETE FROM {$wpdb->prefix}followup_customer_orders"); } set_transient('fue_importing_orders', true, 86400); $sql = "SELECT COUNT( DISTINCT p.id )\n FROM {$wpdb->posts} p, {$wpdb->postmeta} pm\n WHERE p.ID = pm.post_id\n AND p.post_type = 'shop_order'\n AND (SELECT COUNT(*) FROM {$wpdb->postmeta} pm2 WHERE p.ID = pm2.post_id AND pm2.meta_key = '_fue_recorded') = 0"; $total_orders = $wpdb->get_var($sql); if ($total_orders == 0) { update_option('fue_orders_imported', true); delete_transient('fue_importing_orders'); } self::send_response(array('total_orders' => $total_orders)); } else { $sql = "SELECT DISTINCT p.ID\n FROM {$wpdb->posts} p, {$wpdb->postmeta} pm\n WHERE p.ID = pm.post_id\n AND p.post_type = 'shop_order'\n AND (SELECT COUNT(*) FROM {$wpdb->postmeta} pm2 WHERE p.ID = pm2.post_id AND pm2.meta_key = '_fue_recorded') = 0\n LIMIT 1"; $results = $wpdb->get_results($sql); if (count($results) == 0) { update_option('fue_orders_imported', true); delete_transient('fue_importing_orders'); self::send_response(array('status' => 'completed')); } else { $imported = array(); foreach ($results as $row) { $order = WC_FUE_Compatibility::wc_get_order($row->ID); FUE_Addon_Woocommerce::record_order($order); $imported[] = array('id' => $row->ID, 'status' => 'success'); } self::send_response(array('status' => 'partial', 'import_data' => $imported)); } } } }
/** * Get an array of Category IDs included in the given $order * @param int|WC_Order $order * @return array */ protected function get_category_ids_from_order($order) { $wpdb = Follow_Up_Emails::instance()->wpdb; if (is_numeric($order)) { $order = WC_FUE_Compatibility::wc_get_order($order); } if (1 != get_post_meta($order->id, '_fue_recorded', true)) { FUE_Addon_Woocommerce::record_order($order); } $category_ids = $wpdb->get_col($wpdb->prepare("SELECT category_id\n FROM {$wpdb->prefix}followup_order_categories\n WHERE order_id = %d", $order->id)); return array_unique($category_ids); }