From f6f35d466ece0faec1056728a0f4ea72561d3cf0 Mon Sep 17 00:00:00 2001 From: dwindown Date: Sun, 9 Nov 2025 00:23:45 +0700 Subject: [PATCH] fix: Update BOTH enabled sources when toggling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- includes/Api/ShippingController.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/includes/Api/ShippingController.php b/includes/Api/ShippingController.php index cb9a61f..8f84bc6 100644 --- a/includes/Api/ShippingController.php +++ b/includes/Api/ShippingController.php @@ -258,17 +258,21 @@ class ShippingController extends WP_REST_Controller { // Get current settings $method->init_instance_settings(); - $current_settings = $method->instance_settings; - - // Update enabled status - $method->instance_settings['enabled'] = $enabled ? 'yes' : 'no'; - - // Debug: Log settings change - error_log( sprintf( '[WooNooW] Current enabled: %s, New enabled: %s', - isset( $current_settings['enabled'] ) ? $current_settings['enabled'] : 'not set', - $method->instance_settings['enabled'] + + // Debug: Log current state from BOTH sources + error_log( sprintf( '[WooNooW] Current $method->enabled: %s, instance_settings["enabled"]: %s', + isset( $method->enabled ) ? $method->enabled : 'not set', + isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'not set' ) ); + // 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 $option_key = $method->get_instance_option_key(); $saved = update_option( $option_key, $method->instance_settings, 'yes' );