fix: Use correct meta key format for variation attributes

Found the issue from debug log:
- WooCommerce stores as: attribute_Color (exact case match)
- We were trying: attribute_color (lowercase) 

Fixed:
- Use 'attribute_' + exact attribute name
- get_post_meta with true flag returns single value (not array)

Result:
- Variation #362: {"Color": "Red"} 
- Variation #363: {"Color": "Blue"} 

Removed debug logging as requested.
This commit is contained in:
dwindown
2025-11-20 01:03:34 +07:00
parent c397639176
commit 5129ff9aea

View File

@@ -599,10 +599,6 @@ class ProductsController {
// Format attributes with human-readable names and values // Format attributes with human-readable names and values
$formatted_attributes = []; $formatted_attributes = [];
// Debug: Log all meta for this variation
$all_meta = get_post_meta($variation_id);
error_log("Variation #{$variation_id} ALL META: " . print_r($all_meta, true));
// Get parent product attributes to know what to look for // Get parent product attributes to know what to look for
$parent_attributes = $product->get_attributes(); $parent_attributes = $product->get_attributes();
@@ -626,17 +622,10 @@ class ProductsController {
$value = $term ? $term->name : $value; $value = $term ? $term->name : $value;
} }
} else { } else {
// Custom attribute - get the value from variation meta // Custom attribute - WooCommerce stores as 'attribute_' + exact attribute name
// WooCommerce stores it as 'attribute_' + lowercase attribute name $meta_key = 'attribute_' . $attr_name;
$meta_key = 'attribute_' . strtolower($attr_name);
$value = get_post_meta($variation_id, $meta_key, true); $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 // Capitalize the attribute name for display
$clean_name = ucfirst($attr_name); $clean_name = ucfirst($attr_name);
} }