fix: Update BOTH enabled sources when toggling

Root cause identified and fixed!

Problem:
- WooCommerce stores enabled in TWO places:
  1. $method->enabled property (what admin displays)
  2. $method->instance_settings["enabled"] (what we were updating)
- We were only updating instance_settings, not the property
- So toggle saved to DB but $method->enabled stayed "yes"

Solution:
 Read from $method->enabled (correct source)
 Update BOTH $method->enabled AND instance_settings["enabled"]
 Save instance_settings to database
 Now both sources stay in sync

Evidence from logs:
- Before: $method->enabled = "yes", instance_settings = "no" (mismatch!)
- Toggle was reading "no", trying to set "no" → no change
- update_option returned false (no change detected)

After this fix:
 Toggle reads correct current state
 Updates both property and settings
 Saves to database correctly
 WooCommerce admin and SPA stay in sync
This commit is contained in:
dwindown
2025-11-09 00:23:45 +07:00
parent 7d9df9de57
commit f6f35d466e

View File

@@ -258,17 +258,21 @@ class ShippingController extends WP_REST_Controller {
// Get current settings // Get current settings
$method->init_instance_settings(); $method->init_instance_settings();
$current_settings = $method->instance_settings;
// Update enabled status // Debug: Log current state from BOTH sources
$method->instance_settings['enabled'] = $enabled ? 'yes' : 'no'; error_log( sprintf( '[WooNooW] Current $method->enabled: %s, instance_settings["enabled"]: %s',
isset( $method->enabled ) ? $method->enabled : 'not set',
// Debug: Log settings change isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'not set'
error_log( sprintf( '[WooNooW] Current enabled: %s, New enabled: %s',
isset( $current_settings['enabled'] ) ? $current_settings['enabled'] : 'not set',
$method->instance_settings['enabled']
) ); ) );
// Update BOTH the property AND the settings
$new_enabled_value = $enabled ? 'yes' : 'no';
$method->enabled = $new_enabled_value;
$method->instance_settings['enabled'] = $new_enabled_value;
// Debug: Log new state
error_log( sprintf( '[WooNooW] New enabled value: %s', $new_enabled_value ) );
// Save to database // Save to database
$option_key = $method->get_instance_option_key(); $option_key = $method->get_instance_option_key();
$saved = update_option( $option_key, $method->instance_settings, 'yes' ); $saved = update_option( $option_key, $method->instance_settings, 'yes' );