From b3c44a8e6316b6742e37751496bde4411493382b Mon Sep 17 00:00:00 2001 From: dwindown Date: Sat, 8 Nov 2025 23:58:28 +0700 Subject: [PATCH] fix: CRITICAL - Toggle now gets ALL shipping methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed the root cause identified in the audit. Issue: - toggle_method() was calling get_shipping_methods() WITHOUT false parameter - This only returned ENABLED methods by default - Disabled methods were not in the array, so toggle had no effect Solution: ✅ Line 226: get_shipping_methods(false) - gets ALL methods ✅ Simplified settings update (direct assignment vs merge) ✅ Added do_action() hook for WooCommerce compatibility ✅ Better debug logging with option key Changes: - get_shipping_methods() → get_shipping_methods(false) - Removed unnecessary array_merge - Added woocommerce_shipping_zone_method_status_toggled action - Cleaner code structure Result: ✅ Toggle disable: Works correctly ✅ Toggle enable: Works correctly ✅ Refetch shows correct state ✅ WooCommerce compatibility maintained ✅ Other plugins notified via action hook Credit: Audit identified the exact issue on line 226 --- includes/Api/ShippingController.php | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/includes/Api/ShippingController.php b/includes/Api/ShippingController.php index 97b0528..18b4b9e 100644 --- a/includes/Api/ShippingController.php +++ b/includes/Api/ShippingController.php @@ -222,36 +222,37 @@ class ShippingController extends WP_REST_Controller { ); } - // Get all shipping methods for this zone - $shipping_methods = $zone->get_shipping_methods(); + // CRITICAL: Get ALL shipping methods (both enabled and disabled) + $shipping_methods = $zone->get_shipping_methods( false ); $method_found = false; - $option_key = ''; foreach ( $shipping_methods as $method ) { if ( $method->instance_id == $instance_id ) { $method_found = true; - $option_key = $method->get_instance_option_key(); // Debug logging error_log( sprintf( '[WooNooW] Toggling shipping method %s (instance %d) to %s', $method->id, $instance_id, $enabled ? 'enabled' : 'disabled' ) ); - // Get current settings and merge with new enabled status + // Get current settings $method->init_instance_settings(); $current_settings = $method->instance_settings; - $new_settings = array_merge( $current_settings, array( 'enabled' => $enabled ? 'yes' : 'no' ) ); - // Debug: Log current and new 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', - $new_settings['enabled'] + $method->instance_settings['enabled'] ) ); - // Update settings directly - $method->instance_settings = $new_settings; + // Save to database + $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' ) ); - // Save to database using WooCommerce's method - $saved = update_option( $option_key, $new_settings, 'yes' ); - error_log( sprintf( '[WooNooW] update_option returned: %s', $saved ? 'true' : 'false' ) ); + // Fire action hook for other plugins/code + do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method->id, $zone_id, $enabled ); break; }