Prechádzať zdrojové kódy

fix: resolve WooCommerce customer filter API error - convert email to customer ID #76

Claude 5 mesiacov pred
rodič
commit
ec200324f6
1 zmenil súbory, kde vykonal 32 pridanie a 1 odobranie
  1. 32 1
      supabase/functions/mcp-woocommerce/index.ts

+ 32 - 1
supabase/functions/mcp-woocommerce/index.ts

@@ -251,12 +251,43 @@ async function handleListOrders(args: Record<string, any>): Promise<ToolCallResu
     // Build filters
     const filters: WooCommerceOrderFilters = {};
     if (status) filters.status = status;
-    if (customer_email) filters.customer = customer_email;
     if (created_after) filters.after = created_after;
     if (created_before) filters.before = created_before;
     if (updated_after) filters.modified_after = updated_after;
     if (updated_before) filters.modified_before = updated_before;
 
+    // Handle customer_email: WooCommerce requires customer ID (integer), not email
+    if (customer_email) {
+      // First, search for customer by email to get their ID
+      const customers = await fetchCustomers(shop_id, 1, 1, { email: customer_email });
+
+      if (customers.length === 0) {
+        return {
+          content: [{
+            type: 'text',
+            text: JSON.stringify({
+              count: 0,
+              limit: actualLimit,
+              filters_applied: {
+                created_after,
+                created_before,
+                updated_after,
+                updated_before,
+                customer_email,
+                customer_name,
+                status
+              },
+              orders: [],
+              message: `No customer found with email: ${customer_email}`
+            })
+          }]
+        };
+      }
+
+      // Use the customer ID for filtering
+      filters.customer = customers[0].id.toString();
+    }
+
     // Fetch orders from WooCommerce API
     let orders: WooCommerceOrder[];