get(\Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class); if (!$asset_data_registry) { return; } // Get merged fields (default + custom) with positions $merged_fields = $this->get_merged_fields_with_positions(); if (empty($merged_fields)) { return; } // THIS IS THE MAGIC! // Inject field data into WooCommerce Block Checkout $asset_data_registry->add('checkoutFieldsPositions', $merged_fields); } catch (Exception $e) { } } /** * Get WooCommerce default fields merged with custom fields * All fields get their positions from the database */ private function get_merged_fields_with_positions() { $block_manager = CFEP_Block_Field_Manager::instance(); $all_fields = $block_manager->get_all_fields(null, true); // Only enabled $merged = array(); foreach ($all_fields as $field) { $key = $field['field_key']; $position = (int)$field['position']; // Map location to WooCommerce format $location = $field['location']; if ($location === 'address') { $location = 'shipping'; // or 'billing' } elseif ($location === 'contact') { $location = 'contact'; } else { $location = 'additional'; } $merged[$key] = array( 'key' => $key, 'index' => $position, // THIS is what controls order! 'location' => $location, 'label' => $field['label'], 'required' => (bool)$field['required'], 'enabled' => (bool)$field['enabled'], 'type' => $field['field_type'], 'is_default' => isset($field['is_default']) ? (bool)$field['is_default'] : false ); } // Sort by index uasort($merged, function($a, $b) { return $a['index'] - $b['index']; }); return $merged; } }