Created
September 2, 2025 06:55
-
-
Save TanvirHasan19/b7034ee5f4586320dee7a9bc7cbf655e to your computer and use it in GitHub Desktop.
WC Vendors - Regenerate Missing Vendor Orders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * WC Vendors - Regenerate Missing Vendor Orders | |
| * | |
| * This script regenerates missing vendor sub-orders for cases where: | |
| * - Products were initially published under Administrator | |
| * - Product author was later corrected to vendor | |
| * - Commission was recalculated but vendor orders are missing from Orders menu | |
| * | |
| * IMPORTANT: | |
| * - Backup your database before running this script | |
| * - Test on staging environment first | |
| * - This script should be run from WordPress admin or via WP-CLI | |
| * | |
| * Usage: Place this file in your WordPress root and run via browser (admin only) | |
| * Or use WP-CLI: wp eval-file wcv-regenerate-vendor-orders.php | |
| */ | |
| // Prevent direct access | |
| if (!defined('ABSPATH')) { | |
| // If not in WordPress context, try to load WordPress | |
| if (file_exists('./wp-config.php')) { | |
| require_once('./wp-config.php'); | |
| } elseif (file_exists('../wp-config.php')) { | |
| require_once('../wp-config.php'); | |
| } else { | |
| die('WordPress not found. Please run this script from your WordPress root directory.'); | |
| } | |
| } | |
| // Security check - only allow administrators | |
| if (!current_user_can('manage_options')) { | |
| die('Access denied. This script requires administrator privileges.'); | |
| } | |
| /** | |
| * Regenerate vendor orders for a specific order ID | |
| * | |
| * @param int $order_id The parent order ID to process | |
| * @return array Result information | |
| */ | |
| function wcv_regenerate_vendor_orders_for_order($order_id) { | |
| $result = array( | |
| 'success' => false, | |
| 'message' => '', | |
| 'created_orders' => array(), | |
| 'errors' => array() | |
| ); | |
| // Get the order | |
| $order = wc_get_order($order_id); | |
| if (!$order) { | |
| $result['errors'][] = "Order #{$order_id} not found."; | |
| return $result; | |
| } | |
| // Check if this order already has vendor sub-orders | |
| $existing_sub_orders = $order->get_meta('wcv_sub_orders', true); | |
| if (!empty($existing_sub_orders)) { | |
| $result['message'] = "Order #{$order_id} already has vendor sub-orders."; | |
| return $result; | |
| } | |
| // Check if WCV_Vendors class exists | |
| if (!class_exists('WCV_Vendors')) { | |
| $result['errors'][] = "WC Vendors not active or WCV_Vendors class not found."; | |
| return $result; | |
| } | |
| try { | |
| // Use WC Vendors' built-in method to create child orders | |
| $vendor_orders = WCV_Vendors::create_child_orders($order); | |
| if (!empty($vendor_orders)) { | |
| $result['success'] = true; | |
| $result['message'] = "Successfully created " . count($vendor_orders) . " vendor sub-orders for order #{$order_id}."; | |
| foreach ($vendor_orders as $vendor_order) { | |
| $vendor_id = $vendor_order->get_meta('wcv_vendor_id', true); | |
| $vendor_user = get_userdata($vendor_id); | |
| $vendor_name = $vendor_user ? $vendor_user->display_name : "Vendor ID: {$vendor_id}"; | |
| $result['created_orders'][] = array( | |
| 'vendor_order_id' => $vendor_order->get_id(), | |
| 'vendor_id' => $vendor_id, | |
| 'vendor_name' => $vendor_name, | |
| 'commission' => $vendor_order->get_meta('wcv_commission', true) | |
| ); | |
| } | |
| } else { | |
| $result['message'] = "No vendor orders needed for order #{$order_id} (no vendor products found)."; | |
| } | |
| } catch (Exception $e) { | |
| $result['errors'][] = "Error processing order #{$order_id}: " . $e->getMessage(); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * Find orders that might need vendor order regeneration | |
| * | |
| * @param int $limit Number of orders to check (default: 100) | |
| * @return array Order IDs that might need regeneration | |
| */ | |
| function wcv_find_orders_needing_regeneration($limit = 100) { | |
| global $wpdb; | |
| $orders_table = wcv_cot_enabled() ? $wpdb->prefix . 'wc_orders' : $wpdb->prefix . 'posts'; | |
| $meta_table = wcv_cot_enabled() ? $wpdb->prefix . 'wc_orders_meta' : $wpdb->prefix . 'postmeta'; | |
| $id_column = wcv_cot_enabled() ? 'id' : 'ID'; | |
| $type_column = wcv_cot_enabled() ? 'type' : 'post_type'; | |
| $meta_id_column = wcv_cot_enabled() ? 'order_id' : 'post_id'; | |
| // Find orders that have commission entries but no sub-orders | |
| $query = $wpdb->prepare(" | |
| SELECT DISTINCT o.{$id_column} as order_id | |
| FROM {$orders_table} o | |
| INNER JOIN {$wpdb->prefix}pv_commission c ON o.{$id_column} = c.order_id | |
| LEFT JOIN {$meta_table} m ON o.{$id_column} = m.{$meta_id_column} | |
| AND m.meta_key = 'wcv_sub_orders' | |
| WHERE o.{$type_column} = 'shop_order' | |
| AND o.{$id_column} > 0 | |
| AND c.vendor_id > 0 | |
| AND (m.meta_value IS NULL OR m.meta_value = '' OR m.meta_value = 'a:0:{}') | |
| ORDER BY o.{$id_column} DESC | |
| LIMIT %d | |
| ", $limit); | |
| $order_ids = $wpdb->get_col($query); | |
| return $order_ids; | |
| } | |
| // Main execution | |
| if (isset($_GET['action'])) { | |
| switch ($_GET['action']) { | |
| case 'find_orders': | |
| $limit = isset($_GET['limit']) ? intval($_GET['limit']) : 100; | |
| $order_ids = wcv_find_orders_needing_regeneration($limit); | |
| echo "<h2>Orders That May Need Vendor Order Regeneration</h2>"; | |
| if (empty($order_ids)) { | |
| echo "<p>No orders found that need vendor order regeneration.</p>"; | |
| } else { | |
| echo "<p>Found " . count($order_ids) . " orders that may need regeneration:</p>"; | |
| echo "<ul>"; | |
| foreach ($order_ids as $order_id) { | |
| $order = wc_get_order($order_id); | |
| $order_date = $order ? $order->get_date_created()->format('Y-m-d H:i:s') : 'Unknown'; | |
| echo "<li>"; | |
| echo "Order #{$order_id} (Date: {$order_date}) "; | |
| echo "<a href='?action=regenerate_single&order_id={$order_id}'>Regenerate</a>"; | |
| echo "</li>"; | |
| } | |
| echo "</ul>"; | |
| if (count($order_ids) > 0) { | |
| echo "<p><strong>Batch Actions:</strong></p>"; | |
| echo "<a href='?action=regenerate_batch&order_ids=" . implode(',', $order_ids) . "' onclick='return confirm(\"Are you sure you want to regenerate vendor orders for " . count($order_ids) . " orders?\")'>Regenerate All Listed Orders</a>"; | |
| } | |
| } | |
| break; | |
| case 'regenerate_single': | |
| $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0; | |
| if ($order_id > 0) { | |
| $result = wcv_regenerate_vendor_orders_for_order($order_id); | |
| echo "<h2>Regeneration Result for Order #{$order_id}</h2>"; | |
| if ($result['success']) { | |
| echo "<div style='color: green; background: #d4edda; padding: 10px; border: 1px solid #c3e6cb; border-radius: 4px;'>"; | |
| echo "<strong>Success:</strong> " . $result['message']; | |
| if (!empty($result['created_orders'])) { | |
| echo "<h3>Created Vendor Orders:</h3><ul>"; | |
| foreach ($result['created_orders'] as $created_order) { | |
| echo "<li>Vendor Order #{$created_order['vendor_order_id']} for {$created_order['vendor_name']} (Commission: " . wc_price($created_order['commission']) . ")</li>"; | |
| } | |
| echo "</ul>"; | |
| } | |
| echo "</div>"; | |
| } else { | |
| echo "<div style='color: #856404; background: #fff3cd; padding: 10px; border: 1px solid #ffeaa7; border-radius: 4px;'>"; | |
| echo "<strong>Info:</strong> " . $result['message']; | |
| echo "</div>"; | |
| } | |
| if (!empty($result['errors'])) { | |
| echo "<div style='color: #721c24; background: #f8d7da; padding: 10px; border: 1px solid #f5c6cb; border-radius: 4px;'>"; | |
| echo "<strong>Errors:</strong><ul>"; | |
| foreach ($result['errors'] as $error) { | |
| echo "<li>{$error}</li>"; | |
| } | |
| echo "</ul></div>"; | |
| } | |
| } else { | |
| echo "<p>Invalid order ID.</p>"; | |
| } | |
| echo "<p><a href='?action=find_orders'>← Back to Order List</a></p>"; | |
| break; | |
| case 'regenerate_batch': | |
| $order_ids_param = isset($_GET['order_ids']) ? $_GET['order_ids'] : ''; | |
| $order_ids = array_filter(array_map('intval', explode(',', $order_ids_param))); | |
| echo "<h2>Batch Regeneration Results</h2>"; | |
| if (empty($order_ids)) { | |
| echo "<p>No valid order IDs provided.</p>"; | |
| } else { | |
| $total_processed = 0; | |
| $total_success = 0; | |
| $total_created = 0; | |
| foreach ($order_ids as $order_id) { | |
| $result = wcv_regenerate_vendor_orders_for_order($order_id); | |
| $total_processed++; | |
| echo "<h3>Order #{$order_id}</h3>"; | |
| if ($result['success']) { | |
| $total_success++; | |
| $total_created += count($result['created_orders']); | |
| echo "<div style='color: green;'>✓ " . $result['message'] . "</div>"; | |
| } else { | |
| echo "<div style='color: orange;'>ℹ " . $result['message'] . "</div>"; | |
| } | |
| if (!empty($result['errors'])) { | |
| foreach ($result['errors'] as $error) { | |
| echo "<div style='color: red;'>✗ {$error}</div>"; | |
| } | |
| } | |
| } | |
| echo "<div style='background: #e7f3ff; padding: 15px; margin-top: 20px; border-radius: 4px;'>"; | |
| echo "<h3>Summary</h3>"; | |
| echo "<ul>"; | |
| echo "<li>Orders processed: {$total_processed}</li>"; | |
| echo "<li>Orders with vendor orders created: {$total_success}</li>"; | |
| echo "<li>Total vendor orders created: {$total_created}</li>"; | |
| echo "</ul>"; | |
| echo "</div>"; | |
| } | |
| echo "<p><a href='?action=find_orders'>← Back to Order List</a></p>"; | |
| break; | |
| default: | |
| echo "<p>Invalid action.</p>"; | |
| } | |
| } else { | |
| // Show main interface | |
| ?> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>WC Vendors - Regenerate Vendor Orders</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; margin: 20px; } | |
| .button { | |
| display: inline-block; | |
| padding: 10px 20px; | |
| background: #0073aa; | |
| color: white; | |
| text-decoration: none; | |
| border-radius: 4px; | |
| margin: 5px; | |
| } | |
| .button:hover { background: #005a87; } | |
| .warning { | |
| background: #fff3cd; | |
| border: 1px solid #ffeaa7; | |
| padding: 15px; | |
| border-radius: 4px; | |
| margin: 20px 0; | |
| color: #856404; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>WC Vendors - Regenerate Vendor Orders</h1> | |
| <div class="warning"> | |
| <strong>:warning: Important:</strong> This script will regenerate missing vendor sub-orders. | |
| Please ensure you have backed up your database before proceeding. | |
| </div> | |
| <h2>What this script does:</h2> | |
| <ul> | |
| <li>Identifies orders that have commission entries but missing vendor sub-orders</li> | |
| <li>Regenerates the missing vendor sub-orders using WC Vendors' built-in functions</li> | |
| <li>Updates parent order metadata with vendor IDs and sub-order references</li> | |
| <li>Ensures vendor orders appear correctly in the vendor dashboard</li> | |
| </ul> | |
| <h2>Actions:</h2> | |
| <a href="?action=find_orders" class="button">Find Orders Needing Regeneration</a> | |
| <h2>Manual Regeneration:</h2> | |
| <p>If you know specific order IDs that need regeneration, enter them below:</p> | |
| <form method="get"> | |
| <input type="hidden" name="action" value="regenerate_single"> | |
| <input type="number" name="order_id" placeholder="Order ID" required> | |
| <input type="submit" value="Regenerate Single Order" class="button"> | |
| </form> | |
| </body> | |
| </html> | |
| <?php | |
| } | |
| /** | |
| * Alternative SQL-only approach (use with caution) | |
| * | |
| * If the PHP approach doesn't work, you can use this SQL query | |
| * to identify problematic orders: | |
| */ | |
| /* | |
| -- Find orders with commissions but no sub-orders (MySQL) | |
| SELECT DISTINCT o.ID as order_id, | |
| o.post_date as order_date, | |
| COUNT(c.id) as commission_count | |
| FROM wp_posts o | |
| INNER JOIN wp_pv_commission c ON o.ID = c.order_id | |
| LEFT JOIN wp_postmeta m ON o.ID = m.post_id AND m.meta_key = 'wcv_sub_orders' | |
| WHERE o.post_type = 'shop_order' | |
| AND o.post_status IN ('wc-completed', 'wc-processing') | |
| AND c.vendor_id > 0 | |
| AND (m.meta_value IS NULL OR m.meta_value = '' OR m.meta_value = 'a:0:{}') | |
| GROUP BY o.ID | |
| ORDER BY o.ID DESC; | |
| -- For HPOS (Custom Order Tables): | |
| SELECT DISTINCT o.id as order_id, | |
| o.date_created_gmt as order_date, | |
| COUNT(c.id) as commission_count | |
| FROM wp_wc_orders o | |
| INNER JOIN wp_pv_commission c ON o.id = c.order_id | |
| LEFT JOIN wp_wc_orders_meta m ON o.id = m.order_id AND m.meta_key = 'wcv_sub_orders' | |
| WHERE o.type = 'shop_order' | |
| AND o.status IN ('wc-completed', 'wc-processing') | |
| AND c.vendor_id > 0 | |
| AND (m.meta_value IS NULL OR m.meta_value = '' OR m.meta_value = 'a:0:{}') | |
| GROUP BY o.id | |
| ORDER BY o.id DESC; | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment