How to blank all WooCommerce checkout fields by default except country?

本小妞迷上赌 提交于 2021-02-08 02:30:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/55307899/how-to-blank-all-woocommerce-checkout-fields-by-default-except-country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!