diff --git a/includes/Api/ProductsController.php b/includes/Api/ProductsController.php index 715668e..2f2f193 100644 --- a/includes/Api/ProductsController.php +++ b/includes/Api/ProductsController.php @@ -167,12 +167,14 @@ class ProductsController { 'page' => $page, 'per_page' => $per_page, 'pages' => $query->max_num_pages, + '_debug' => 'ProductsController-v2-' . time(), // Verify this code is running ], 200); // Prevent caching $response->header('Cache-Control', 'no-cache, no-store, must-revalidate'); $response->header('Pragma', 'no-cache'); $response->header('Expires', '0'); + $response->header('X-WooNooW-Version', '2.0'); // Debug header return $response; } @@ -579,29 +581,35 @@ class ProductsController { // Format attributes with human-readable names and values $formatted_attributes = []; - $variation_attributes = $variation->get_attributes(); - // Debug: Log raw variation attributes - error_log('WooNooW Debug - Variation #' . $variation_id . ' raw attributes: ' . print_r($variation_attributes, true)); + // Get parent product attributes to know what to look for + $parent_attributes = $product->get_attributes(); - 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 + foreach ($parent_attributes as $parent_attr) { + if (!$parent_attr->get_variation()) { + continue; // Skip non-variation attributes } - $formatted_attributes[$clean_name] = $attr_value; + $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) { + // Global/taxonomy attribute + $clean_name = wc_attribute_label($attr_name); + $value = $variation->get_attribute($attr_name); + + // Convert slug to term name + if (!empty($value)) { + $term = get_term_by('slug', $value, $attr_name); + $value = $term ? $term->name : $value; + } + } else { + // Custom attribute - get from meta + $value = get_post_meta($variation_id, 'attribute_' . sanitize_title($attr_name), true); + } + + $formatted_attributes[$clean_name] = $value; } $variations[] = [