From fa4c4a1402bbab44f04fc070a5f8673fd37406ef Mon Sep 17 00:00:00 2001 From: dwindown Date: Sun, 9 Nov 2025 00:33:00 +0700 Subject: [PATCH] fix: Clear zone cache after toggle to force reload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added aggressive cache clearing after toggle. Issue: - update_option saves to DB correctly - But $method->enabled is loaded when zone object is created - Zone object is cached, so it keeps old enabled value - Next request loads cached zone with old enabled="yes" Solution: ✅ Save instance_settings to DB ✅ Delete shipping method count transient ✅ Clear shipping_zones cache (all zones) ✅ Clear specific zone cache by ID ✅ Update $method->enabled in memory ✅ Clear global shipping cache version This forces WooCommerce to: 1. Reload zone from database 2. Reload methods from database 3. Read fresh enabled value 4. Display correct state Test: Toggle should now persist correctly --- includes/Api/ShippingController.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/includes/Api/ShippingController.php b/includes/Api/ShippingController.php index 8f84bc6..85fc526 100644 --- a/includes/Api/ShippingController.php +++ b/includes/Api/ShippingController.php @@ -265,9 +265,8 @@ class ShippingController extends WP_REST_Controller { isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'not set' ) ); - // Update BOTH the property AND the settings + // Update the settings array with new enabled value $new_enabled_value = $enabled ? 'yes' : 'no'; - $method->enabled = $new_enabled_value; $method->instance_settings['enabled'] = $new_enabled_value; // Debug: Log new state @@ -277,6 +276,14 @@ class ShippingController extends WP_REST_Controller { $option_key = $method->get_instance_option_key(); $saved = update_option( $option_key, $method->instance_settings, 'yes' ); error_log( sprintf( '[WooNooW] update_option(%s) returned: %s', $option_key, $saved ? 'true' : 'false' ) ); + + // CRITICAL: Clear the zone's method cache so it reloads from DB + delete_transient( 'wc_shipping_method_count' ); + wp_cache_delete( 'shipping_zones', 'woocommerce' ); + wp_cache_delete( $zone_id, 'shipping_zones' ); + + // Also update the in-memory property so WooCommerce sees it immediately + $method->enabled = $new_enabled_value; // Fire action hook for other plugins/code do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method->id, $zone_id, $enabled );