You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除WordPress主页中WooCommerce的get_customer_location Ajax请求

How to Remove the 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.php file or a custom plugin (never edit core WooCommerce files—your changes will be lost during updates).
  • The 100 priority 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:32:13