From 304a58d8a159c63a24609b58f96ff9287ba19947 Mon Sep 17 00:00:00 2001 From: dwindown Date: Thu, 20 Nov 2025 00:32:42 +0700 Subject: [PATCH] fix: Force fresh data fetch and improve variation attribute handling Fixed 2 issues: 1. Frontend Showing Stale Data - FIXED Problem: Table shows "Simple" even though API returns "variable" Root Cause: React Query caching old data Solution (index.tsx): - Added staleTime: 0 (always fetch fresh) - Added gcTime: 0 (don't cache) - Forces React Query to fetch from API every time Result: Table will show correct product type 2. Variation Attribute Values - IMPROVED Problem: attributes show { "Color": "" } instead of { "Color": "Red" } Improvements: - Use wc_attribute_label() for proper attribute names - Better handling of global vs custom attributes - Added debug logging to see raw WooCommerce data Debug Added: - Logs raw variation attributes to debug.log - Check: wp-content/debug.log - Shows what WooCommerce actually returns Note: If attribute values still empty, it means: - Variations not properly saved in WooCommerce - Need to re-save product or regenerate variations Test: 1. Refresh products page 2. Should show correct type (variable) 3. Check debug.log for variation attribute data 4. If still empty, re-save the variable product --- admin-spa/src/routes/Products/index.tsx | 2 ++ includes/Api/ProductsController.php | 30 +++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/admin-spa/src/routes/Products/index.tsx b/admin-spa/src/routes/Products/index.tsx index 994b3ef..33b852c 100644 --- a/admin-spa/src/routes/Products/index.tsx +++ b/admin-spa/src/routes/Products/index.tsx @@ -88,6 +88,8 @@ export default function Products() { order, }), placeholderData: keepPreviousData, + staleTime: 0, // Always fetch fresh data + gcTime: 0, // Don't cache }); // Fetch categories for filter diff --git a/includes/Api/ProductsController.php b/includes/Api/ProductsController.php index a5cb82d..715668e 100644 --- a/includes/Api/ProductsController.php +++ b/includes/Api/ProductsController.php @@ -579,16 +579,28 @@ class ProductsController { // Format attributes with human-readable names and values $formatted_attributes = []; - foreach ($variation->get_attributes() as $attr_name => $attr_value) { - // Remove 'pa_' prefix if present (for global attributes) - $clean_name = str_replace('pa_', '', $attr_name); - // Capitalize first letter - $clean_name = ucfirst($clean_name); - // Get term name if it's a taxonomy - if (taxonomy_exists($attr_name)) { - $term = get_term_by('slug', $attr_value, $attr_name); - $attr_value = $term ? $term->name : $attr_value; + $variation_attributes = $variation->get_attributes(); + + // Debug: Log raw variation attributes + error_log('WooNooW Debug - Variation #' . $variation_id . ' raw attributes: ' . print_r($variation_attributes, true)); + + foreach ($variation_attributes as $attr_name => $attr_value) { + // Handle taxonomy attributes (pa_*) + if (strpos($attr_name, 'pa_') === 0) { + // Global attribute + $taxonomy = $attr_name; + $clean_name = wc_attribute_label($taxonomy); + + if (!empty($attr_value)) { + $term = get_term_by('slug', $attr_value, $taxonomy); + $attr_value = $term ? $term->name : $attr_value; + } + } else { + // Custom attribute + $clean_name = ucfirst(str_replace('_', ' ', $attr_name)); + // For custom attributes, the value is already the display value } + $formatted_attributes[$clean_name] = $attr_value; }