如何在 Wordpress 中强制使用默认国家/地区

How to force default country in Wordpress

我的 wordpress 不需要国家字段,所以我在用户注册时取消设置它的字段。但是它与航运区域冲突,因为国家字段没有值。有没有办法在没有用户干预的情况下强制设置默认国家/地区。

请在 functions.php

中尝试此代码
    //set default country in checkout
    add_filter( 'default_checkout_billing_country', 'change_default_billing_country', 10, 1 );
    add_filter( 'default_checkout_shipping_country', 'change_default_shipping_country', 10, 1 );
    
    function change_default_billing_country( $country ) {
        // If the user already exists, don't override country
        if ( WC()->customer->get_is_paying_customer() ) {
            return $country;
        }
    
        $country = "US";
        return $country;  
    }
    
    function change_default_shipping_country( $country ) {
        // If the user already exists, don't override country
        if ( WC()->customer->get_is_paying_customer() ) {
            return $country;
        }
        $country = "US";
        return $country; 
    }
    
    add_filter( 'default_checkout_billing_country', 'change_default_checkout_country_and_state' );
    function change_default_checkout_country_and_state( $default ) {
        if(is_user_logged_in())
        {
            $billcountry = get_user_meta( get_current_user_id() , 'billing_country', true );
            if($billcountry == '')
            {
                $billcountry = "US";
            }
        }
        else
        {
          $billcountry = "US"; 
        }
        return $billcountry;
    }

   //hide country field in shipping calculator and checkout page
    add_action( 'wp_footer', 'hide_calc_country_cart' ); 
     
    function hide_calc_country_cart() {
       if ( is_cart()) {
          echo "<script type='text/javascript'>
                $('[id=\"calc_shipping_country_field\"]').css('display','none');
         </script>";
       }
       if ( is_checkout()) {
          echo "<script type='text/javascript'>
                $('[id=\"billing_country_field\"]').css('display','none');
         </script>";
       }
    }