fix: Add no-cache headers and fix variation attribute display
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"
This commit is contained in:
@@ -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] : '',
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user