fix: CRITICAL - Toggle now gets ALL shipping methods

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
This commit is contained in:
dwindown
2025-11-08 23:58:28 +07:00
parent a83d3dc3a3
commit b3c44a8e63

View File

@@ -222,36 +222,37 @@ class ShippingController extends WP_REST_Controller {
); );
} }
// Get all shipping methods for this zone // CRITICAL: Get ALL shipping methods (both enabled and disabled)
$shipping_methods = $zone->get_shipping_methods(); $shipping_methods = $zone->get_shipping_methods( false );
$method_found = false; $method_found = false;
$option_key = '';
foreach ( $shipping_methods as $method ) { foreach ( $shipping_methods as $method ) {
if ( $method->instance_id == $instance_id ) { if ( $method->instance_id == $instance_id ) {
$method_found = true; $method_found = true;
$option_key = $method->get_instance_option_key();
// Debug logging // Debug logging
error_log( sprintf( '[WooNooW] Toggling shipping method %s (instance %d) to %s', $method->id, $instance_id, $enabled ? 'enabled' : 'disabled' ) ); 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(); $method->init_instance_settings();
$current_settings = $method->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', error_log( sprintf( '[WooNooW] Current enabled: %s, New enabled: %s',
isset( $current_settings['enabled'] ) ? $current_settings['enabled'] : 'not set', isset( $current_settings['enabled'] ) ? $current_settings['enabled'] : 'not set',
$new_settings['enabled'] $method->instance_settings['enabled']
) ); ) );
// Update settings directly // Save to database
$method->instance_settings = $new_settings; $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 // Fire action hook for other plugins/code
$saved = update_option( $option_key, $new_settings, 'yes' ); do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method->id, $zone_id, $enabled );
error_log( sprintf( '[WooNooW] update_option returned: %s', $saved ? 'true' : 'false' ) );
break; break;
} }