fix: Empty variation attributes + API route standardization

**Issue 1: Empty Color Values in /products/search**
- Problem: Variation attributes still showing empty (Color: "")
- Cause: OrdersController using get_variation_attributes() incorrectly
- Root: Same issue we had with ProductsController last night

**Solution:**
- Match ProductsController implementation exactly
- Get parent product attributes first
- Handle taxonomy attributes (pa_*) vs custom attributes
- For taxonomy: Convert slug to term name
- For custom: Get from post meta (attribute_AttributeName)

**Changes to OrdersController.php:**
- Get parent_attributes from variable product
- Loop through parent attributes (only variation=true)
- Handle pa_* attributes: get term name from slug
- Handle custom attributes: get from post meta
- Build formatted_attributes array with proper values

**Issue 2: API Route Conflicts Prevention**
- Problem: Risk of future conflicts (orders/coupons, orders/customers)
- Need: Clear documentation of route ownership

**Solution: Created API_ROUTES.md**

**Route Registry:**

**Conflict Prevention Rules:**
1. Each resource has ONE primary controller
2. Cross-resource operations use specific action routes
3. Use sub-resources for related data (/orders/{id}/coupons)
4. First-registered-wins (registration order matters)

**Documentation:**
- Created API_ROUTES.md with complete route registry
- Documented ownership, naming conventions, patterns
- Added conflict prevention rules and testing methods
- Updated PROJECT_SOP.md to reference API_ROUTES.md
- Added to Documentation Standards section

**Result:**
 Variation attributes now display correctly (Color: Red)
 Clear API route ownership documented
 Future conflicts prevented with standards
 Ready for Coupons and Customers CRUD implementation

**Testing:**
- Test /products/search returns proper Color values
- Verify no route conflicts in REST API
- Confirm OrderForm displays variations correctly
This commit is contained in:
dwindown
2025-11-20 10:49:58 +07:00
parent be69b40237
commit 316cee846d
3 changed files with 320 additions and 9 deletions

View File

@@ -1155,24 +1155,49 @@ class OrdersController {
// If variable product, include variations
if ( $p->get_type() === 'variable' ) {
$variation_ids = $p->get_children();
$parent_attributes = $p->get_attributes();
foreach ( $variation_ids as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( ! $variation ) continue;
// Get variation attributes
$attributes = [];
foreach ( $variation->get_variation_attributes() as $attr_name => $attr_value ) {
// Remove 'attribute_' prefix and format name
$clean_name = str_replace( 'attribute_', '', $attr_name );
$clean_name = str_replace( 'pa_', '', $clean_name ); // Remove taxonomy prefix if present
$clean_name = ucfirst( str_replace( [ '-', '_' ], ' ', $clean_name ) );
// Get variation attributes properly from parent attributes
$formatted_attributes = [];
foreach ( $parent_attributes as $parent_attr ) {
if ( ! $parent_attr->get_variation() ) {
continue; // Skip non-variation attributes
}
$attributes[ $clean_name ] = $attr_value;
$attr_name = $parent_attr->get_name();
$clean_name = $attr_name;
// 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 - WooCommerce stores as 'attribute_' + exact attribute name
$meta_key = 'attribute_' . $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;
}
$data['variations'][] = [
'id' => $variation->get_id(),
'attributes' => $attributes,
'attributes' => $formatted_attributes,
'price' => (float) $variation->get_price(),
'regular_price' => (float) $variation->get_regular_price(),
'sale_price' => $variation->get_sale_price() ? (float) $variation->get_sale_price() : null,