From 5d0f887c4b8c8e89f797dc1f17f117cb9dcda0d3 Mon Sep 17 00:00:00 2001 From: dwindown Date: Thu, 20 Nov 2025 00:26:54 +0700 Subject: [PATCH] fix: Add no-cache headers and fix variation attribute display MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed 2 critical issues: 1. API Response Caching - FIXED Problem: API returns old data without type, status fields Root Cause: WordPress REST API response caching Solution: - Added no-cache headers to response: * Cache-Control: no-cache, no-store, must-revalidate * Pragma: no-cache * Expires: 0 - Added debug logging to verify data structure - Forces fresh data on every request Result: API will return fresh data with all fields 2. Variation Attribute Values Missing - FIXED Problem: Shows "color:" instead of "color: Red" Root Cause: API returns slugs not human-readable values Before: attributes: { "pa_color": "red" } After: attributes: { "Color": "Red" } Solution: - Remove 'pa_' prefix from attribute names - Capitalize attribute names - Convert taxonomy slugs to term names - Return human-readable format Code: - Clean name: pa_color → Color - Get term: red (slug) → Red (name) - Format: { Color: Red } Result: Variations show "color: Red" correctly Test: 1. Hard refresh browser (Ctrl+Shift+R or Cmd+Shift+R) 2. Check products list - should show type and prices 3. Edit variable product - should show "color: Red" --- includes/Api/ProductsController.php | 34 ++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/includes/Api/ProductsController.php b/includes/Api/ProductsController.php index 07fd2fe..a5cb82d 100644 --- a/includes/Api/ProductsController.php +++ b/includes/Api/ProductsController.php @@ -152,17 +152,29 @@ class ProductsController { foreach ($query->posts as $post) { $product = wc_get_product($post->ID); if ($product) { - $products[] = self::format_product_list_item($product); + $formatted = self::format_product_list_item($product); + // Debug: Log first product to verify structure + if (empty($products)) { + error_log('WooNooW Debug - First product data: ' . print_r($formatted, true)); + } + $products[] = $formatted; } } - return new WP_REST_Response([ + $response = new WP_REST_Response([ 'rows' => $products, 'total' => $query->found_posts, 'page' => $page, 'per_page' => $per_page, 'pages' => $query->max_num_pages, ], 200); + + // Prevent caching + $response->header('Cache-Control', 'no-cache, no-store, must-revalidate'); + $response->header('Pragma', 'no-cache'); + $response->header('Expires', '0'); + + return $response; } /** @@ -564,6 +576,22 @@ class ProductsController { $variation = wc_get_product($variation_id); if ($variation) { $image = wp_get_attachment_image_src($variation->get_image_id(), 'thumbnail'); + + // 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; + } + $formatted_attributes[$clean_name] = $attr_value; + } + $variations[] = [ 'id' => $variation->get_id(), 'sku' => $variation->get_sku(), @@ -573,7 +601,7 @@ class ProductsController { 'stock_status' => $variation->get_stock_status(), 'stock_quantity' => $variation->get_stock_quantity(), 'manage_stock' => $variation->get_manage_stock(), - 'attributes' => $variation->get_attributes(), + 'attributes' => $formatted_attributes, 'image_id' => $variation->get_image_id(), 'image_url' => $image ? $image[0] : '', ];