/** * Remove duplicate sub-orders if found * * @since 2.4.4 * * @return json success|error|data */ function check_duplicate_suborders() { if (session_id() == '') { session_start(); } global $wpdb; parse_str($_POST['data'], $data); if (!wp_verify_nonce($data['_wpnonce'], 'regen_sync_table')) { wp_send_json_error(); } $limit = $data['limit']; $offset = $data['offset']; $prev_done = $_POST['done']; $total_orders = isset($_POST['total_orders']) ? $_POST['total_orders'] : 0; if ($offset == 0) { unset($_SESSION['dokan_duplicate_order_ids']); $total_orders = $wpdb->get_var("SELECT count(ID) FROM {$wpdb->posts} AS p\n LEFT JOIN {$wpdb->postmeta} AS m ON p.ID = m.post_id \n WHERE post_type = 'shop_order' AND m.meta_key = 'has_sub_order'"); } $sql = "SELECT ID FROM {$wpdb->posts} AS p\n LEFT JOIN {$wpdb->postmeta} AS m ON p.ID = m.post_id \n WHERE post_type = 'shop_order' AND m.meta_key = 'has_sub_order'\n LIMIT %d,%d"; $orders = $wpdb->get_results($wpdb->prepare($sql, $offset * $limit, $limit)); $duplicate_orders = isset($_SESSION['dokan_duplicate_order_ids']) ? $_SESSION['dokan_duplicate_order_ids'] : array(); if ($orders) { foreach ($orders as $order) { $sellers_count = count(dokan_get_sellers_by($order->ID)); $sub_order_ids = dokan_get_suborder_ids_by($order->ID); if ($sellers_count < count($sub_order_ids)) { $duplicate_orders = array_merge(array_slice($sub_order_ids, $sellers_count), $duplicate_orders); } } if (count($duplicate_orders)) { $_SESSION['dokan_duplicate_order_ids'] = $duplicate_orders; } $done = $prev_done + count($orders); wp_send_json_success(array('offset' => $offset + 1, 'total_orders' => $total_orders, 'done' => $done, 'message' => sprintf(__('%d orders checked out of %d', 'dokan'), $done, $total_orders))); } else { if (count($duplicate_orders)) { wp_send_json_success(array('offset' => 0, 'done' => 'All', 'message' => sprintf(__('All orders are checked and we found some duplicate orders', 'dokan')), 'duplicate' => true)); } $dashboard_link = sprintf('<a href="%s">%s</a>', admin_url('admin.php?page=dokan'), __('Go to Dashboard →', 'dokan')); wp_send_json_success(array('offset' => 0, 'done' => 'All', 'message' => sprintf(__('All orders are checked and no duplicate was found. %s', 'dokan'), $dashboard_link))); } }
/** * Monitors a new order and attempts to create sub-orders * * If an order contains products from multiple vendor, we can't show the order * to each seller dashboard. That's why we need to divide the main order to * some sub-orders based on the number of sellers. * * @param int $parent_order_id * @return void */ function dokan_create_sub_order($parent_order_id) { if (get_post_meta($parent_order_id, 'has_sub_order') == true) { $args = array('post_parent' => $parent_order_id, 'post_type' => 'shop_order', 'numberposts' => -1, 'post_status' => 'any'); $child_orders = get_children($args); foreach ($child_orders as $child) { wp_delete_post($child->ID); } } $parent_order = new WC_Order($parent_order_id); $sellers = dokan_get_sellers_by($parent_order_id); // return if we've only ONE seller if (count($sellers) == 1) { $temp = array_keys($sellers); $seller_id = reset($temp); wp_update_post(array('ID' => $parent_order_id, 'post_author' => $seller_id)); return; } // flag it as it has a suborder update_post_meta($parent_order_id, 'has_sub_order', true); // seems like we've got multiple sellers foreach ($sellers as $seller_id => $seller_products) { dokan_create_seller_order($parent_order, $seller_id, $seller_products); } }
/** * Monitors a new order and attempts to create sub-orders * * If an order contains products from multiple vendor, we can't show the order * to each seller dashboard. That's why we need to divide the main order to * some sub-orders based on the number of sellers. * * @param int $parent_order_id * @return void */ function dokan_create_sub_order($parent_order_id) { if (get_post_meta($parent_order_id, 'has_sub_order') == true) { return; } $sellers = dokan_get_sellers_by($parent_order_id); // return if we've only ONE seller if (count($sellers) == 1) { $temp = array_keys($sellers); $seller_id = reset($temp); wp_update_post(array('ID' => $parent_order_id, 'post_author' => $seller_id)); return; } // flag it as it has a suborder update_post_meta($parent_order_id, 'has_sub_order', true); // seems like we've got multiple sellers foreach ($sellers as $seller_id => $seller_products) { dokan_create_seller_order($parent_order, $seller_id, $seller_products); } }