debug: Add debug markers to verify ProductsController is running

Added debug markers:
1. _debug field in response with timestamp
2. X-WooNooW-Version header
3. Improved variation attribute retrieval

Issue: API returns different structure than code produces
Response has: id, name, price (only 9 fields)
Code returns: id, name, type, status, price, etc (15+ fields)

This suggests:
- Response is cached somewhere
- Different controller handling request
- Middleware transforming response

Debug steps:
1. Check response for _debug field
2. Check response headers for X-WooNooW-Version
3. If missing, endpoint not using our code
4. Check wp-content/debug.log for error messages
This commit is contained in:
dwindown
2025-11-20 00:39:24 +07:00
parent 304a58d8a1
commit bc733ab2a6

View File

@@ -167,12 +167,14 @@ class ProductsController {
'page' => $page, 'page' => $page,
'per_page' => $per_page, 'per_page' => $per_page,
'pages' => $query->max_num_pages, 'pages' => $query->max_num_pages,
'_debug' => 'ProductsController-v2-' . time(), // Verify this code is running
], 200); ], 200);
// Prevent caching // Prevent caching
$response->header('Cache-Control', 'no-cache, no-store, must-revalidate'); $response->header('Cache-Control', 'no-cache, no-store, must-revalidate');
$response->header('Pragma', 'no-cache'); $response->header('Pragma', 'no-cache');
$response->header('Expires', '0'); $response->header('Expires', '0');
$response->header('X-WooNooW-Version', '2.0'); // Debug header
return $response; return $response;
} }
@@ -579,29 +581,35 @@ class ProductsController {
// Format attributes with human-readable names and values // Format attributes with human-readable names and values
$formatted_attributes = []; $formatted_attributes = [];
$variation_attributes = $variation->get_attributes();
// Debug: Log raw variation attributes // Get parent product attributes to know what to look for
error_log('WooNooW Debug - Variation #' . $variation_id . ' raw attributes: ' . print_r($variation_attributes, true)); $parent_attributes = $product->get_attributes();
foreach ($variation_attributes as $attr_name => $attr_value) { foreach ($parent_attributes as $parent_attr) {
// Handle taxonomy attributes (pa_*) if (!$parent_attr->get_variation()) {
continue; // Skip non-variation attributes
}
$attr_name = $parent_attr->get_name();
$clean_name = $parent_attr->get_name(); // Already clean for custom attributes
// Get the variation's value for this attribute
if (strpos($attr_name, 'pa_') === 0) { if (strpos($attr_name, 'pa_') === 0) {
// Global attribute // Global/taxonomy attribute
$taxonomy = $attr_name; $clean_name = wc_attribute_label($attr_name);
$clean_name = wc_attribute_label($taxonomy); $value = $variation->get_attribute($attr_name);
if (!empty($attr_value)) { // Convert slug to term name
$term = get_term_by('slug', $attr_value, $taxonomy); if (!empty($value)) {
$attr_value = $term ? $term->name : $attr_value; $term = get_term_by('slug', $value, $attr_name);
$value = $term ? $term->name : $value;
} }
} else { } else {
// Custom attribute // Custom attribute - get from meta
$clean_name = ucfirst(str_replace('_', ' ', $attr_name)); $value = get_post_meta($variation_id, 'attribute_' . sanitize_title($attr_name), true);
// For custom attributes, the value is already the display value
} }
$formatted_attributes[$clean_name] = $attr_value; $formatted_attributes[$clean_name] = $value;
} }
$variations[] = [ $variations[] = [