2

On my WooCommerce checkout page, I want the billing fields to be blank except for the billing country.

I'm using this to make sure the checkout form is blank when it gets filled out:

add_filter('woocommerce_checkout_get_value','__return_empty_string',10);

However, I do want the billing country field to be filled out, and default to the US. So I've got this:

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );

function change_default_checkout_country() {
  return 'US'; 
}

But the field appears blank, as long as I have that first filter in place. How can I have all the fields blank except the billing country?

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
trose1189
  • 21
  • 2

1 Answers1

0

Try the following instead, that will blank all checkout fields, except billing and shipping countries that will be set for a specific country:

add_filter('woocommerce_checkout_get_value', 'checkout_get_value_filter', 10, 2 );
function checkout_get_value_filter( $value, $input ) {
    // The defined country code
    $country_code = 'US';

    // Set the billing and shipping country
    WC()->customer->set_billing_country($country_code);
    WC()->customer->set_shipping_country($country_code);

    // Display only the billing and shipping country
    if( 'billing_country' === $input || 'shipping_country' === $input ){
        return $country_code;
    } else {
        return '';
    }
}

Code goes on function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399