fix: Read/write enabled status directly from database option
CRITICAL FIX: Bypass cached instance_settings completely. Root Cause Found: - $method->instance_settings["enabled"] = "no" (stale/wrong) - $method->enabled = "yes" (correct, from somewhere else) - DB option actually has enabled="yes" - instance_settings is a CACHED copy that is stale Solution: ✅ Read: get_option($option_key) directly (bypass cache) ✅ Write: update_option($option_key) directly ✅ Don't use instance_settings at all Why instance_settings was wrong: - init_instance_settings() loads from cache - Cache is stale/not synced with DB - WooCommerce admin uses different code path - That code path reads fresh from DB Now we: 1. Read current value from DB: get_option() 2. Modify the array 3. Save back to DB: update_option() 4. Clear caches 5. Done! Test: This should finally work!
This commit is contained in:
@@ -101,19 +101,22 @@ class ShippingController extends WP_REST_Controller {
|
|||||||
// Get fresh settings from database
|
// Get fresh settings from database
|
||||||
$method->init_instance_settings();
|
$method->init_instance_settings();
|
||||||
|
|
||||||
// Debug: Check BOTH sources of enabled status
|
// CRITICAL: The enabled property comes from init_settings, not init_instance_settings
|
||||||
|
// We need to check the actual settings property
|
||||||
|
$settings = get_option( $method->get_instance_option_key(), array() );
|
||||||
|
$is_enabled = isset( $settings['enabled'] ) && $settings['enabled'] === 'yes';
|
||||||
|
|
||||||
|
// Debug: Check ALL sources of enabled status
|
||||||
error_log( sprintf(
|
error_log( sprintf(
|
||||||
'[WooNooW] Zone %d Method %s (instance %d): $method->enabled = "%s", instance_settings["enabled"] = "%s"',
|
'[WooNooW] Zone %d Method %s (instance %d): $method->enabled = "%s", instance_settings["enabled"] = "%s", DB option["enabled"] = "%s"',
|
||||||
$zone_data['id'],
|
$zone_data['id'],
|
||||||
$method->id,
|
$method->id,
|
||||||
$method->instance_id,
|
$method->instance_id,
|
||||||
isset( $method->enabled ) ? $method->enabled : 'NOT SET',
|
isset( $method->enabled ) ? $method->enabled : 'NOT SET',
|
||||||
isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'NOT SET'
|
isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'NOT SET',
|
||||||
|
isset( $settings['enabled'] ) ? $settings['enabled'] : 'NOT SET'
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Use $method->enabled as the source of truth (this is what WooCommerce uses)
|
|
||||||
$is_enabled = isset( $method->enabled ) && $method->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,
|
||||||
@@ -157,18 +160,20 @@ class ShippingController extends WP_REST_Controller {
|
|||||||
// Get fresh settings from database
|
// Get fresh settings from database
|
||||||
$method->init_instance_settings();
|
$method->init_instance_settings();
|
||||||
|
|
||||||
// Debug: Check BOTH sources of enabled status
|
// CRITICAL: Read directly from database option
|
||||||
|
$settings = get_option( $method->get_instance_option_key(), array() );
|
||||||
|
$is_enabled = isset( $settings['enabled'] ) && $settings['enabled'] === 'yes';
|
||||||
|
|
||||||
|
// Debug: Check ALL sources of enabled status
|
||||||
error_log( sprintf(
|
error_log( sprintf(
|
||||||
'[WooNooW] Zone 0 (Rest of World) Method %s (instance %d): $method->enabled = "%s", instance_settings["enabled"] = "%s"',
|
'[WooNooW] Zone 0 (Rest of World) Method %s (instance %d): $method->enabled = "%s", instance_settings["enabled"] = "%s", DB option["enabled"] = "%s"',
|
||||||
$method->id,
|
$method->id,
|
||||||
$method->instance_id,
|
$method->instance_id,
|
||||||
isset( $method->enabled ) ? $method->enabled : 'NOT SET',
|
isset( $method->enabled ) ? $method->enabled : 'NOT SET',
|
||||||
isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'NOT SET'
|
isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'NOT SET',
|
||||||
|
isset( $settings['enabled'] ) ? $settings['enabled'] : 'NOT SET'
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Use $method->enabled as the source of truth (this is what WooCommerce uses)
|
|
||||||
$is_enabled = isset( $method->enabled ) && $method->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,
|
||||||
@@ -256,25 +261,24 @@ class ShippingController extends WP_REST_Controller {
|
|||||||
// 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
|
// Get current settings DIRECTLY from database (not from cached instance_settings)
|
||||||
$method->init_instance_settings();
|
$option_key = $method->get_instance_option_key();
|
||||||
|
$current_settings = get_option( $option_key, array() );
|
||||||
|
|
||||||
// Debug: Log current state from BOTH sources
|
// Debug: Log current state from database
|
||||||
error_log( sprintf( '[WooNooW] Current $method->enabled: %s, instance_settings["enabled"]: %s',
|
error_log( sprintf( '[WooNooW] Current DB option[enabled]: %s',
|
||||||
isset( $method->enabled ) ? $method->enabled : 'not set',
|
isset( $current_settings['enabled'] ) ? $current_settings['enabled'] : 'not set'
|
||||||
isset( $method->instance_settings['enabled'] ) ? $method->instance_settings['enabled'] : 'not set'
|
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Update the settings array with new enabled value
|
// Update the enabled value in the settings array
|
||||||
$new_enabled_value = $enabled ? 'yes' : 'no';
|
$new_enabled_value = $enabled ? 'yes' : 'no';
|
||||||
$method->instance_settings['enabled'] = $new_enabled_value;
|
$current_settings['enabled'] = $new_enabled_value;
|
||||||
|
|
||||||
// Debug: Log new state
|
// Debug: Log new state
|
||||||
error_log( sprintf( '[WooNooW] New enabled value: %s', $new_enabled_value ) );
|
error_log( sprintf( '[WooNooW] New enabled value: %s', $new_enabled_value ) );
|
||||||
|
|
||||||
// Save to database
|
// Save to database
|
||||||
$option_key = $method->get_instance_option_key();
|
$saved = update_option( $option_key, $current_settings, 'yes' );
|
||||||
$saved = update_option( $option_key, $method->instance_settings, 'yes' );
|
|
||||||
error_log( sprintf( '[WooNooW] update_option(%s) returned: %s', $option_key, $saved ? 'true' : 'false' ) );
|
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: Clear the zone's method cache so it reloads from DB
|
||||||
|
|||||||
Reference in New Issue
Block a user