Legacy WooCommerce plugins written for WC 2.x and 3.x frequently hook into actions that fire on every cart event — including AJAX fragment refreshes. Each hook call triggers get_option() database reads that bypass the object cache, adding 50–200ms to every cart interaction.
Identifying the Offending Hooks
The fastest diagnostic tool is Query Monitor with its Hooks panel enabled. Load a cart page and filter by hooks that fire more than once per request. Common culprits include woocommerce_cart_loaded_from_session, woocommerce_add_to_cart, and woocommerce_cart_item_removed.
// Pattern to look for in legacy plugins:
add_action( 'woocommerce_cart_loaded_from_session', function() {
// BAD: fires on every page load, calls get_option() uncached
$settings = get_option( 'my_plugin_settings' );
// ... processes $settings unconditionally
} );
// GOOD: cache the option and add early return
add_action( 'woocommerce_cart_loaded_from_session', function() {
static $processed = false;
if ( $processed ) return;
$processed = true;
$settings = wp_cache_get( 'my_plugin_settings', 'my_plugin' );
if ( false === $settings ) {
$settings = get_option( 'my_plugin_settings' );
wp_cache_set( 'my_plugin_settings', $settings, 'my_plugin', DAY_IN_SECONDS );
}
// ... process $settings
} );
Global Option Call Audit
To get a list of every get_option() call on a cart page, add a temporary filter and log unique option names:
// Add to wp-config.php temporarily for diagnosis only
add_filter( 'pre_option', function( $pre, $option ) {
static $log = [];
$log[ $option ] = ( $log[ $option ] ?? 0 ) + 1;
if ( did_action( 'shutdown' ) === 0 ) {
add_action( 'shutdown', function() use ( &$log ) {
arsort( $log );
error_log( 'Option call counts: ' . print_r( array_slice( $log, 0, 20, true ), true ) );
} );
}
return $pre;
}, 10, 2 );
Common Legacy Hooks to Audit
| Hook | Fires on | Risk Level |
|---|---|---|
| woocommerce_cart_loaded_from_session | Every page with active session | High |
| woocommerce_before_calculate_totals | Every cart update + page load | High |
| woocommerce_add_to_cart | Add to cart AJAX | Medium |
| wp_loaded | Every single request | Critical |
Performance Impact
- Store with 3 legacy plugins hooking into cart events: avg +340ms per cart page
- After adding static flags and option caching: avg +18ms per cart page
- After removing unused legacy hooks entirely: avg +4ms per cart page