From 86525a32e3e5004221e8c51d7eec62c2834ef602 Mon Sep 17 00:00:00 2001 From: dwindown Date: Thu, 20 Nov 2025 01:00:50 +0700 Subject: [PATCH] fix: Properly extract variation attribute values from WooCommerce meta MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed empty attribute values in variations. WooCommerce stores variation attributes in post meta: - Key format: 'attribute_' + lowercase attribute name - Example: 'attribute_color' → 'red' Changes: 1. Try lowercase: attribute_color 2. Fallback to sanitized: attribute_pa-color 3. Capitalize name for display: Color This should now show: - Before: {"Color": ""} - After: {"Color": "Red"} or {"Color": "Blue"} Test by refreshing edit product page. --- includes/Api/ProductsController.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/includes/Api/ProductsController.php b/includes/Api/ProductsController.php index dbf7a20..4d9a373 100644 --- a/includes/Api/ProductsController.php +++ b/includes/Api/ProductsController.php @@ -608,7 +608,7 @@ class ProductsController { } $attr_name = $parent_attr->get_name(); - $clean_name = $parent_attr->get_name(); // Already clean for custom attributes + $clean_name = $attr_name; // Get the variation's value for this attribute if (strpos($attr_name, 'pa_') === 0) { @@ -622,8 +622,19 @@ class ProductsController { $value = $term ? $term->name : $value; } } else { - // Custom attribute - get from meta - $value = get_post_meta($variation_id, 'attribute_' . sanitize_title($attr_name), true); + // Custom attribute - get the value from variation meta + // WooCommerce stores it as 'attribute_' + lowercase attribute name + $meta_key = 'attribute_' . strtolower($attr_name); + $value = get_post_meta($variation_id, $meta_key, true); + + // If not found, try with sanitized title + if (empty($value)) { + $meta_key = 'attribute_' . sanitize_title($attr_name); + $value = get_post_meta($variation_id, $meta_key, true); + } + + // Capitalize the attribute name for display + $clean_name = ucfirst($attr_name); } $formatted_attributes[$clean_name] = $value;