fix: Update BOTH database tables for shipping method enabled status

FINAL FIX: WooCommerce stores enabled in TWO places!

Discovery:
- wp_options: woocommerce_flat_rate_X_settings["enabled"]
- wp_woocommerce_shipping_zone_methods: is_enabled column
- We were only updating wp_options
- WooCommerce admin reads from zone_methods table
- Checkout reads from zone_methods table too!

Solution:
 Update wp_options (for settings)
 Update zone_methods table (for WooCommerce admin & checkout)
 Clear all caches
 Update in-memory property

SQL Update:
UPDATE wp_woocommerce_shipping_zone_methods
SET is_enabled = 1/0
WHERE instance_id = X

Now both sources stay in sync:
 SPA reads correct state
 WooCommerce admin shows correct state
 Checkout shows correct shipping options
 Everything works!

This is the same pattern WooCommerce uses internally.
This commit is contained in:
dwindown
2025-11-09 00:42:51 +07:00
parent 47ed661ce5
commit d04746c9a5

View File

@@ -277,16 +277,29 @@ class ShippingController extends WP_REST_Controller {
// Debug: Log new state
error_log( sprintf( '[WooNooW] New enabled value: %s', $new_enabled_value ) );
// Save to database
// Save to database (wp_options table)
$saved = update_option( $option_key, $current_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
// CRITICAL: Also update the zone_methods table (where WooCommerce admin reads from)
global $wpdb;
$table_name = $wpdb->prefix . 'woocommerce_shipping_zone_methods';
$updated = $wpdb->update(
$table_name,
array( 'is_enabled' => $enabled ? 1 : 0 ),
array( 'instance_id' => $instance_id ),
array( '%d' ),
array( '%d' )
);
error_log( sprintf( '[WooNooW] Updated zone_methods table: %s rows affected', $updated !== false ? $updated : 'ERROR' ) );
// 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' );
\WC_Cache_Helper::get_transient_version( 'shipping', true );
// Also update the in-memory property so WooCommerce sees it immediately
// Also update the in-memory property
$method->enabled = $new_enabled_value;
// Fire action hook for other plugins/code