fix: Shipping toggle now works correctly

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
This commit is contained in:
dwindown
2025-11-08 22:26:16 +07:00
parent 380170096c
commit 24dbd625db

View File

@@ -93,17 +93,21 @@ class ShippingController extends WP_REST_Controller {
} }
} }
// Get shipping methods for this zone // Get shipping methods for this zone (false = get all, not just enabled)
$shipping_methods = $zone->get_shipping_methods( true ); // true = enabled only $shipping_methods = $zone->get_shipping_methods( false );
$rates = array(); $rates = array();
foreach ( $shipping_methods as $method ) { 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( $rate = array(
'id' => $method->id . ':' . $method->instance_id, 'id' => $method->id . ':' . $method->instance_id,
'instance_id' => $method->instance_id, 'instance_id' => $method->instance_id,
'method_id' => $method->id, 'method_id' => $method->id,
'name' => $method->get_title(), 'name' => $method->get_title(),
'enabled' => $method->enabled === 'yes', 'enabled' => $is_enabled,
); );
// Get cost if available // Get cost if available
@@ -134,16 +138,20 @@ class ShippingController extends WP_REST_Controller {
// Add "Rest of the World" zone (zone 0) // Add "Rest of the World" zone (zone 0)
$zone_0 = new \WC_Shipping_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(); $rates = array();
foreach ( $shipping_methods as $method ) { 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( $rate = array(
'id' => $method->id . ':' . $method->instance_id, 'id' => $method->id . ':' . $method->instance_id,
'instance_id' => $method->instance_id, 'instance_id' => $method->instance_id,
'method_id' => $method->id, 'method_id' => $method->id,
'name' => $method->get_title(), 'name' => $method->get_title(),
'enabled' => $method->enabled === 'yes', 'enabled' => $is_enabled,
); );
if ( isset( $method->cost ) && $method->cost !== '' ) { if ( isset( $method->cost ) && $method->cost !== '' ) {