如何移除WordPress主页中WooCommerce的get_customer_location Ajax请求
get_customer_location HTTP Request from WordPress Homepage Hey there! I see you’re already doing great work cleaning up unnecessary WooCommerce scripts on non-store pages, and now you want to get rid of that https://www.example.com?wc-ajax=get_customer_location request. Let’s fix that together.
First, that request comes from WooCommerce’s automatic geolocation feature, which tries to detect a visitor’s location even on non-WooCommerce pages. Even with your existing script cleanup, the geolocation-related AJAX handlers or scripts might still be active. Here are two targeted solutions:
Solution 1: Deregister Geolocation Script & Remove AJAX Handlers
Add this function after your existing grd_woocommerce_script_cleaner function to directly block the request source:
// Disable get_customer_location AJAX request on non-WooCommerce pages function disable_get_customer_location_ajax() { // Target non-store pages (homepage, blog, etc.) if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) { // Remove AJAX handlers for both logged-in and guest users remove_action( 'wp_ajax_get_customer_location', array( WC_Geolocation::class, 'get_customer_location' ) ); remove_action( 'wp_ajax_nopriv_get_customer_location', array( WC_Geolocation::class, 'get_customer_location' ) ); // Deregister the geolocation script that triggers the request wp_dequeue_script( 'wc-geolocation' ); wp_deregister_script( 'wc-geolocation' ); } } add_action( 'wp_enqueue_scripts', 'disable_get_customer_location_ajax', 100 ); // Higher priority to override WooCommerce
Solution 2: Disable Geolocation Detection Entirely (For Non-Store Pages)
If the first method doesn’t fully resolve it, you can disable WooCommerce’s geolocation feature outright on non-WooCommerce pages:
// Turn off automatic geolocation on non-WooCommerce pages function disable_woocommerce_geolocation() { if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) { add_filter( 'woocommerce_geolocation_enabled', '__return_false' ); add_filter( 'woocommerce_geolocation_auto_detect', '__return_false' ); } } add_action( 'template_redirect', 'disable_woocommerce_geolocation' );
Quick Tips:
- Add these functions to your child theme’s
functions.phpfile or a custom plugin (never edit core WooCommerce files—your changes will be lost during updates). - The
100priority on the first function ensures it runs after your existing cleanup script, so it can properly target the geolocation assets.
Combine these with your existing script cleaner, and that get_customer_location request should disappear from your homepage and other non-WooCommerce pages.
内容的提问来源于stack exchange,提问作者Sandro

