Commit 70deca19c9f for woocommerce
commit 70deca19c9fd9dc505916bb76c22a33178a5129e
Author: Oleksandr Aratovskyi <79862886+oaratovskyi@users.noreply.github.com>
Date: Mon Nov 23 17:37:33 2026 +0200
Fix settings fields with a nested field_name showing default values (#70297)
* Fix settings fields with a nested field_name showing default values
* Add changelog entry for nested settings field values fix
* Remove overlapping field_name read test cases
* Fix field_name reads for option names that parse_str() rewrites
* Keep reading settings fields without an ID through the ID
save_fields() skips fields without an ID, so there is no stored value to align the read with. Reading field_name for them only changed what custom field callbacks receive.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Add tests for field_name read precedence and explicit values
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Read field_name for settings fields with an empty ID
save_fields() saves a field when 'id' is set, even to an empty string. Check that before output_fields() fills in a missing ID, so only fields without one keep reading through the ID.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep reading settings fields with is_option set to false through the ID
save_fields() skips these fields, so nothing it saves is stored under their field_name. Use the same skip rules when deciding whether to read field_name.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Fix field_name reads for encoded keys, split names and legacy update actions
Derive the stored key from parse_str() so percent-encoded keys match what save_fields() writes, and fall back to the ID when parse_str() splits the name instead of throwing a TypeError. Read types that save_fields() hands to a legacy woocommerce_update_option_{type} action through the ID.
* Remove overlapping field_name read test cases
Keep one case per code path: drop rows that take the same branch as another case, and move the rewritten option name case into the read data provider.
* Test empty and zero values stored under a settings field name
Without these rows, loosening the stored-value check to a truthiness
check passes every test while cleared fields fall back to the default.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Read settings named 0 or blank through the field ID
save_fields() writes a setting name of 0 over the whole option, and
update_option() rejects option names that trim to '' or '0', so the
form must not read those locations.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Update plugins/woocommerce/changelog/fix-48254-settings-field-name-value
Co-authored-by: Daniel Mallory <daniel.mallory@automattic.com>
* Resolve settings field names in one helper
Move the field_name parsing in get_field_value() into resolve_field_name(), so the read through the field ID happens in one place.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Explain why a settings field name value is read twice
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Test settings field reads against what save_fields() stores
Save through save_fields() before rendering in the tests for skipped fields, dotted names and settings named 0, and check what it stored, so a change to save_fields() fails a test instead of letting the form read the wrong place.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Test the stored-option array check with a non-zero setting name
The 'string option' row used test_string[0], which the lookup rejects
before the is_array() check, so it repeated the 'setting named 0' row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Update the settings doc for field_name values in the admin form
The admin form now shows the value stored under field_name, so the
advice to set value on such fields no longer applies.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Remove the settings doc sentence about the form reading the field ID
The admin form now shows the value stored under field_name, so the
sentence telling extensions to set value was wrong. Removing it avoids
adding new claims about the form's fallback cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Daniel Mallory <daniel.mallory@automattic.com>
diff --git a/docs/extensions/settings-and-config/extend-wc-settings-page.md b/docs/extensions/settings-and-config/extend-wc-settings-page.md
index 2162d30ac10..7482dd23845 100644
--- a/docs/extensions/settings-and-config/extend-wc-settings-page.md
+++ b/docs/extensions/settings-and-config/extend-wc-settings-page.md
@@ -396,6 +396,6 @@ The same metadata works on email form fields exposed through a settings group. I
**Writes.** Each item sends either `value` or, for `multiselect` and `multi_select_countries` fields, `values` with a list. Fields with a non-null `fixed_value` or a `value` supplied by the definition are refused, since no option stores them. Values go through the same per-type validation as the REST settings API, then must match a shape that `woocommerce/settings-query` can return. Select and multiselect values must be one of the field's choices, country fields must be known country or country/state codes, and page fields must be a published page ID or empty. As in the admin form, cart, checkout and My account cannot share a page, the terms page cannot be the checkout page, the refund and returns policy cannot use the cart, checkout, shop or My account page, and changing a field whose ID ends in `_endpoint` queues a rewrite rules flush. Fields with a string option key are saved through `WC_Admin_Settings::save_fields()`, so the `woocommerce_admin_settings_sanitize_option` filters run as they do in the admin form.
-Updates are all or nothing: if any requested ID is unknown, not opted in or has an invalid value, the ability returns one error and saves nothing. Unknown and withheld IDs return the same `woocommerce_ability_settings_not_writable` error, so callers cannot probe for hidden fields. Each saved setting returns its `previous` value, so a change can be undone. For a field with `field_name`, the abilities read and write the option the admin form saves to, the one named by `field_name`. The admin form displays the option named by `id` unless the definition supplies `value`, so set `value` on such fields to keep the screen in step. Like the REST settings update endpoint, the ability checks only the edit permission, so permission to edit a setting also lets the caller read the value it replaces. The ability is annotated as destructive, since each change overwrites a stored value.
+Updates are all or nothing: if any requested ID is unknown, not opted in or has an invalid value, the ability returns one error and saves nothing. Unknown and withheld IDs return the same `woocommerce_ability_settings_not_writable` error, so callers cannot probe for hidden fields. Each saved setting returns its `previous` value, so a change can be undone. For a field with `field_name`, the abilities read and write the option the admin form saves to, the one named by `field_name`. Like the REST settings update endpoint, the ability checks only the edit permission, so permission to edit a setting also lets the caller read the value it replaces. The ability is annotated as destructive, since each change overwrites a stored value.
Existing REST settings reads and writes retain their current behavior.
diff --git a/plugins/woocommerce/changelog/fix-48254-settings-field-name-value b/plugins/woocommerce/changelog/fix-48254-settings-field-name-value
new file mode 100644
index 00000000000..3f06e2f1d0f
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-48254-settings-field-name-value
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show saved values instead of defaults for extension settings fields that are saved under a different option name.
diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-settings.php b/plugins/woocommerce/includes/admin/class-wc-admin-settings.php
index ff47cc59f03..8e35044d694 100644
--- a/plugins/woocommerce/includes/admin/class-wc-admin-settings.php
+++ b/plugins/woocommerce/includes/admin/class-wc-admin-settings.php
@@ -241,6 +241,78 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) :
return ( null === $option_value ) ? $default : $option_value;
}
+ /**
+ * Get the stored value of a settings field.
+ *
+ * When save_fields() saves a field that sets 'field_name', it stores the value under that name, so read it from there.
+ * Fields it skips, other names, and values not stored there are read through the field ID as before.
+ *
+ * @param array $field Field definition with 'id', 'type', 'field_name' and 'default' set.
+ * @param bool $is_saved Whether save_fields() saves this field.
+ * @return mixed
+ */
+ private static function get_field_value( $field, $is_saved ) {
+ $field_name = $field['field_name'];
+
+ if (
+ $is_saved
+ && $field_name !== $field['id']
+ && is_string( $field_name )
+ && is_string( $field['type'] )
+ // save_fields() hands types with a legacy update action to that action instead of saving them.
+ && ! has_action( 'woocommerce_update_option_' . sanitize_title( $field['type'] ) )
+ ) {
+ $location = self::resolve_field_name( $field_name );
+ if ( null !== $location ) {
+ list( $option_name, $setting_name ) = $location;
+
+ $stored = get_option( $option_name, null );
+ if ( null !== $setting_name ) {
+ $stored = is_array( $stored ) ? ( $stored[ $setting_name ] ?? null ) : null;
+ }
+
+ if ( null !== $stored && ! is_object( $stored ) ) {
+ // Read it again through self::get_option() so it is unslashed the same way as a read by ID.
+ return self::get_option( $field_name, $field['default'] );
+ }
+ }
+ }
+
+ return self::get_option( $field['id'], $field['default'] );
+ }
+
+ /**
+ * Get the option and setting name that save_fields() stores a field name under.
+ *
+ * @param string $field_name Field name, like 'my_option' or 'my_options[enabled]'.
+ * @return array{string, string|null}|null Option and setting name, or null when save_fields() can't store the field there.
+ */
+ private static function resolve_field_name( string $field_name ): ?array {
+ if ( ! preg_match( '/^([^\[\]]+)(?:\[([^\[\]]+)\])?$/', $field_name, $matches ) ) {
+ return null;
+ }
+
+ $option_name = $matches[1];
+ $setting_name = null;
+ if ( isset( $matches[2] ) ) {
+ // Resolve the name the way save_fields() does: parse_str() stores 'a.b[c]' under 'a_b'.
+ parse_str( $field_name, $parsed_name );
+ $option_name = (string) key( $parsed_name );
+ $parsed_option = $parsed_name[ $option_name ] ?? null;
+ if ( ! is_array( $parsed_option ) ) {
+ return null;
+ }
+ $setting_name = (string) key( $parsed_option );
+ }
+
+ // save_fields() writes a setting name of '0' over the whole option, and update_option() rejects names that trim to '' or '0'.
+ if ( empty( trim( $option_name ) ) || '0' === $setting_name ) {
+ return null;
+ }
+
+ return array( $option_name, $setting_name );
+ }
+
/**
* Output admin fields.
*
@@ -253,6 +325,8 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) :
if ( ! isset( $value['type'] ) ) {
continue;
}
+ // Same skip rules as save_fields(), checked before a missing ID is filled in.
+ $is_saved = isset( $value['id'] ) && ! ( isset( $value['is_option'] ) && false === $value['is_option'] );
if ( ! isset( $value['id'] ) ) {
$value['id'] = '';
}
@@ -294,7 +368,7 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) :
$value['suffix'] = '';
}
if ( ! isset( $value['value'] ) ) {
- $value['value'] = self::get_option( $value['id'], $value['default'] );
+ $value['value'] = self::get_field_value( $value, $is_saved );
}
if ( ! is_null( $value['fixed_value'] ?? null ) ) {
diff --git a/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-settings-test.php b/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-settings-test.php
index 5230a8b9e06..581d657b385 100644
--- a/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-settings-test.php
+++ b/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-settings-test.php
@@ -575,6 +575,241 @@ class WC_Admin_Settings_Test extends WC_Unit_Test_Case {
$this->assertSame( 1, $xpath->query( $radio_title . '/span[normalize-space(.)="Object ID radio"]' )->length );
}
+ /**
+ * @testdox Should render the value save_fields() stored under a field name, or the ID value when it overwrote the whole option.
+ *
+ * @dataProvider saved_field_name_data
+ *
+ * @param string $field_name Field name of the saved field.
+ * @param array $posted Posted form data, as PHP parses it from the request.
+ * @param string $option_name Option save_fields() should write.
+ * @param mixed $expected_stored Value that option should hold after the save.
+ * @param string $expected Value the rendered input should carry.
+ */
+ public function test_output_fields_renders_value_saved_through_field_name( string $field_name, array $posted, string $option_name, $expected_stored, string $expected ): void {
+ update_option( 'test_field_id', 'id value' );
+ $options = array(
+ array(
+ 'id' => 'test_field_id',
+ 'field_name' => $field_name,
+ 'type' => 'text',
+ 'default' => 'default value',
+ ),
+ );
+
+ WC_Admin_Settings::save_fields( $options, $posted );
+
+ $this->assertSame( $expected_stored, get_option( $option_name ) );
+ $this->assertStringContainsString( 'value="' . $expected . '"', $this->render_fields( $options ) );
+ }
+
+ /**
+ * Data provider for test_output_fields_renders_value_saved_through_field_name().
+ *
+ * @return array<string, array{string, array<string, mixed>, string, mixed, string}>
+ */
+ public static function saved_field_name_data(): array {
+ return array(
+ 'nested name' => array( 'test_parent[key]', array( 'test_parent' => array( 'key' => 'saved value' ) ), 'test_parent', array( 'key' => 'saved value' ), 'saved value' ),
+ // PHP turns dots and spaces in a posted name into underscores, and save_fields() does the same through parse_str().
+ 'rewritten option name' => array( 'test.dotted options[one]', array( 'test_dotted_options' => array( 'one' => 'saved value' ) ), 'test_dotted_options', array( 'one' => 'saved value' ), 'saved value' ),
+ // save_fields() writes a setting named 0 over the whole option, so the form reads the ID instead.
+ 'setting named 0' => array( 'test_parent[0]', array( 'test_parent' => array( 0 => 'saved value' ) ), 'test_parent', 'saved value', 'id value' ),
+ );
+ }
+
+ /**
+ * @testdox Should read through the field name only when a value is stored there, and through the ID otherwise.
+ *
+ * @dataProvider field_name_read_data
+ *
+ * @param array $stored_options Options to store, keyed by option name.
+ * @param mixed $field_name Field name of the rendered field.
+ * @param string $expected Value the rendered input should carry.
+ */
+ public function test_output_fields_reads_through_field_name_or_id( array $stored_options, $field_name, string $expected ): void {
+ foreach ( $stored_options as $option_name => $option_value ) {
+ update_option( $option_name, $option_value );
+ }
+
+ $output = $this->render_fields(
+ array(
+ array(
+ 'id' => 'test_field_id',
+ 'field_name' => $field_name,
+ 'type' => 'text',
+ 'default' => 'default value',
+ ),
+ )
+ );
+
+ $this->assertStringContainsString( 'value="' . $expected . '"', $output );
+ }
+
+ /**
+ * Data provider for test_output_fields_reads_through_field_name_or_id().
+ *
+ * @return array<string, array{array<string, mixed>, mixed, string}>
+ */
+ public static function field_name_read_data(): array {
+ $id_value = array( 'test_field_id' => 'id value' );
+
+ return array(
+ 'plain name' => array( array( 'test_plain' => 'plain value' ) + $id_value, 'test_plain', 'plain value' ),
+ 'empty value under name' => array( array( 'test_plain' => '' ) + $id_value, 'test_plain', '' ),
+ 'zero under nested name' => array( array( 'test_parent' => array( 'key' => '0' ) ) + $id_value, 'test_parent[key]', '0' ),
+ 'setting named 0' => array( array( 'test_parent' => array( 0 => 'other value' ) ) + $id_value, 'test_parent[0]', 'id value' ),
+ 'option named 0' => array( $id_value, '0', 'id value' ),
+ 'option named 0 with space' => array( $id_value, ' 0', 'id value' ),
+ 'encoded nested key' => array( array( 'test_parent' => array( 'A' => 'decoded value' ) ) + $id_value, 'test_parent[%41]', 'decoded value' ),
+ 'nothing stored under name' => array( $id_value, 'test_parent[key]', 'id value' ),
+ 'two levels' => array( array( 'test_parent' => array( 'deep' => array( 'leaf' => 'deep value' ) ) ) + $id_value, 'test_parent[deep][leaf]', 'id value' ),
+ 'string option' => array( array( 'test_string' => 'abc' ) + $id_value, 'test_string[1]', 'id value' ),
+ 'object value under key' => array( array( 'test_object' => array( 'key' => new stdClass() ) ) + $id_value, 'test_object[key]', 'id value' ),
+ 'name split by &' => array( array( 'test_split' => array( 'key' => 'other value' ) ) + $id_value, 'test_split&other[key]', 'id value' ),
+ );
+ }
+
+ /**
+ * @testdox Should pass the ID value to a custom field type that save_fields() hands to a legacy update action instead of saving.
+ */
+ public function test_output_fields_reads_through_id_for_types_with_legacy_update_action(): void {
+ $this->setExpectedDeprecated( 'The woocommerce_update_option_X action' );
+ update_option( 'test_field_id', 'id value' );
+ update_option( 'test_parent', array( 'key' => 'nested value' ) );
+ add_action( 'woocommerce_update_option_test_legacy_type', function () {} );
+ $received = null;
+ add_action(
+ 'woocommerce_admin_field_test_legacy_type',
+ function ( $field ) use ( &$received ) {
+ $received = $field['value'];
+ }
+ );
+ $options = array(
+ array(
+ 'id' => 'test_field_id',
+ 'field_name' => 'test_parent[key]',
+ 'type' => 'test_legacy_type',
+ 'default' => 'default value',
+ ),
+ );
+
+ WC_Admin_Settings::save_fields( $options, array( 'test_parent' => array( 'key' => 'posted value' ) ) );
+ $this->render_fields( $options );
+
+ $this->assertSame( array( 'key' => 'nested value' ), get_option( 'test_parent' ) );
+ $this->assertSame( 'id value', $received );
+ }
+
+ /**
+ * @testdox Should render an explicit empty value without reading the stored values.
+ */
+ public function test_output_fields_renders_explicit_value_over_stored_values(): void {
+ update_option( 'test_field_id', 'id value' );
+ update_option( 'test_parent', array( 'key' => 'nested value' ) );
+
+ $output = $this->render_fields(
+ array(
+ array(
+ 'id' => 'test_field_id',
+ 'field_name' => 'test_parent[key]',
+ 'type' => 'text',
+ 'default' => 'default value',
+ 'value' => '',
+ ),
+ )
+ );
+
+ $this->assertStringContainsString( 'value=""', $output );
+ }
+
+ /**
+ * @testdox Should render the value saved through a nested field name for a field with an empty ID.
+ */
+ public function test_output_fields_renders_value_saved_for_empty_id(): void {
+ $options = array(
+ array(
+ 'id' => '',
+ 'field_name' => 'test_parent[key]',
+ 'type' => 'text',
+ 'default' => 'default value',
+ ),
+ );
+
+ WC_Admin_Settings::save_fields( $options, array( 'test_parent' => array( 'key' => 'saved value' ) ) );
+
+ $this->assertSame( array( 'key' => 'saved value' ), get_option( 'test_parent' ) );
+ $this->assertStringContainsString( 'value="saved value"', $this->render_fields( $options ) );
+ }
+
+ /**
+ * @testdox Should read a field with is_option set to false through its ID, since save_fields() doesn't save it under its field name.
+ */
+ public function test_output_fields_reads_non_option_field_through_id(): void {
+ update_option( 'test_field_id', 'id value' );
+ update_option( 'test_parent', array( 'key' => 'nested value' ) );
+ $options = array(
+ array(
+ 'id' => 'test_field_id',
+ 'field_name' => 'test_parent[key]',
+ 'type' => 'text',
+ 'default' => 'default value',
+ 'is_option' => false,
+ ),
+ );
+
+ WC_Admin_Settings::save_fields( $options, array( 'test_parent' => array( 'key' => 'posted value' ) ) );
+
+ $this->assertSame( array( 'key' => 'nested value' ), get_option( 'test_parent' ) );
+ $this->assertStringContainsString( 'value="id value"', $this->render_fields( $options ) );
+ }
+
+ /**
+ * @testdox Should pass the default to a custom field without an ID, since save_fields() doesn't save it under its field name.
+ */
+ public function test_output_fields_passes_default_to_id_less_custom_field(): void {
+ update_option( 'test_parent', array( 'rules' => array( 'stored rule' ) ) );
+
+ $received = null;
+ add_action(
+ 'woocommerce_admin_field_test_custom',
+ function ( $field ) use ( &$received ) {
+ $received = $field['value'];
+ }
+ );
+ $options = array(
+ array(
+ 'type' => 'test_custom',
+ 'field_name' => 'test_parent[rules]',
+ 'default' => 'default value',
+ ),
+ );
+
+ WC_Admin_Settings::save_fields( $options, array( 'test_parent' => array( 'rules' => 'posted rule' ) ) );
+ $this->render_fields( $options );
+
+ $this->assertSame( array( 'rules' => array( 'stored rule' ) ), get_option( 'test_parent' ) );
+ $this->assertSame( 'default value', $received );
+ }
+
+ /**
+ * Render settings fields and return the output.
+ *
+ * @param array $options Field definitions.
+ * @return string
+ */
+ private function render_fields( array $options ): string {
+ ob_start();
+ try {
+ WC_Admin_Settings::output_fields( $options );
+ $output = (string) ob_get_contents();
+ } finally {
+ ob_end_clean();
+ }
+
+ return $output;
+ }
+
/**
* @testdox Should render each stored General setting back into its own form field.
*/