默认情况下,如何清空所有WooCommerce结帐字段(国家/地区除外)? [英] How to blank all WooCommerce checkout fields by default except country?

查看:93
本文介绍了默认情况下,如何清空所有WooCommerce结帐字段(国家/地区除外)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

代码在您的活动子主题(或活动主题)的function.php文件上.经过测试,可以正常工作.

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

这篇关于默认情况下,如何清空所有WooCommerce结帐字段(国家/地区除外)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆