如何默认清空除国家/地区以外的所有 WooCommerce 结帐字段?

2024-05-06

在我的 WooCommerce 结帐页面上,我希望除帐单国家/地区之外的帐单字段为空白。

我用它来确保结账表格在填写时是空白的:

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

但是,我确实希望填写帐单国家/地区字段,并默认为美国。所以我有这个:

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );

function change_default_checkout_country() {
  return 'US'; 
}

但只要我设置了第一个过滤器,该字段就会显示为空白。如何将除帐单所在国家/地区之外的所有字段都留空?


请尝试以下操作,这将清空所有结帐字段,但将为特定国家/地区设置的帐单和送货国家/地区除外:

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 '';
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何默认清空除国家/地区以外的所有 WooCommerce 结帐字段? 的相关文章

随机推荐