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
This commit is contained in:
dwindown
2025-11-20 00:32:42 +07:00
parent 5d0f887c4b
commit 304a58d8a1
2 changed files with 23 additions and 9 deletions

View File

@@ -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

View File

@@ -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;
}