info

Affiliate Disclosure: This article contains benchmarked tools that may compensate us. We only recommend hardware and software tested in our lab environments for maximum LCP and INP efficiency.

Legacy Action Hooks: Identifying Plugin Hooks That Kill Cart Performance

Identifying legacy plugin hooks that trigger unnecessary global option calls on every single cart event.

calendar_today folder

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

HookFires onRisk Level
woocommerce_cart_loaded_from_sessionEvery page with active sessionHigh
woocommerce_before_calculate_totalsEvery cart update + page loadHigh
woocommerce_add_to_cartAdd to cart AJAXMedium
wp_loadedEvery single requestCritical

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