fix: Properly extract variation attribute values from WooCommerce meta

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.
This commit is contained in:
dwindown
2025-11-20 01:00:50 +07:00
parent f75f4c6e33
commit 86525a32e3

View File

@@ -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;