fix: Clear zone cache after toggle to force reload

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
This commit is contained in:
dwindown
2025-11-09 00:33:00 +07:00
parent f6f35d466e
commit fa4c4a1402

View File

@@ -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 );