From 24dbd625dbd57a8385e0e0e4af6821ab72dc2a2f Mon Sep 17 00:00:00 2001 From: dwindown Date: Sat, 8 Nov 2025 22:26:16 +0700 Subject: [PATCH] fix: Shipping toggle now works correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed the root cause of toggle not working. Issue: - get_shipping_methods(true) only returns ENABLED methods - When we disabled a method, it disappeared from the list - Refetch showed old data because disabled methods were filtered out Solution: ✅ Use get_shipping_methods(false) to get ALL methods ✅ Read fresh enabled status from instance_settings ✅ Call init_instance_settings() to get latest data from DB ✅ Check enabled field properly: instance_settings["enabled"] === "yes" Result: ✅ Toggle disable: method stays in list with enabled=false ✅ Toggle enable: method shows enabled=true ✅ Refetch shows correct state ✅ WooCommerce settings page reflects changes ✅ No more lying optimistic feedback --- includes/Api/ShippingController.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/includes/Api/ShippingController.php b/includes/Api/ShippingController.php index 0189e81..97b0528 100644 --- a/includes/Api/ShippingController.php +++ b/includes/Api/ShippingController.php @@ -93,17 +93,21 @@ class ShippingController extends WP_REST_Controller { } } - // Get shipping methods for this zone - $shipping_methods = $zone->get_shipping_methods( true ); // true = enabled only + // Get shipping methods for this zone (false = get all, not just enabled) + $shipping_methods = $zone->get_shipping_methods( false ); $rates = array(); foreach ( $shipping_methods as $method ) { + // Get fresh settings from database + $method->init_instance_settings(); + $is_enabled = isset( $method->instance_settings['enabled'] ) && $method->instance_settings['enabled'] === 'yes'; + $rate = array( 'id' => $method->id . ':' . $method->instance_id, 'instance_id' => $method->instance_id, 'method_id' => $method->id, 'name' => $method->get_title(), - 'enabled' => $method->enabled === 'yes', + 'enabled' => $is_enabled, ); // Get cost if available @@ -134,16 +138,20 @@ class ShippingController extends WP_REST_Controller { // Add "Rest of the World" zone (zone 0) $zone_0 = new \WC_Shipping_Zone( 0 ); - $shipping_methods = $zone_0->get_shipping_methods( true ); + $shipping_methods = $zone_0->get_shipping_methods( false ); $rates = array(); foreach ( $shipping_methods as $method ) { + // Get fresh settings from database + $method->init_instance_settings(); + $is_enabled = isset( $method->instance_settings['enabled'] ) && $method->instance_settings['enabled'] === 'yes'; + $rate = array( 'id' => $method->id . ':' . $method->instance_id, 'instance_id' => $method->instance_id, 'method_id' => $method->id, 'name' => $method->get_title(), - 'enabled' => $method->enabled === 'yes', + 'enabled' => $is_enabled, ); if ( isset( $method->cost ) && $method->cost !== '' ) {