WooCommerce:在结账页面默认设置国家

7

我在我的WordPress网站中使用WooCommerce。在结账页面上,默认情况下会填充客户的账单和送货地址。我希望国家不会被默认设置。即使用户已登录,也将要求选择国家。

Screenshot link

有什么建议吗?为了实现这个目标我应该做些什么?

5个回答

6

更新(自WooCommerce 3以来)

以下是用于国家(可选状态)的WooCommerce钩子和代码:

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_country_and_state' );
function change_default_checkout_country_and_state( $default ) {
    return null;
}

甚至可以更简短:
add_filter( 'default_checkout_billing_country', '__return_null' );
add_filter( 'default_checkout_shipping_country', '__return_null' );
add_filter( 'default_checkout_billing_state', '__return_null' );
add_filter( 'default_checkout_shipping_state', '__return_null' );

代码放在当前子主题(或活动主题)的functions.php文件中。已经测试并且可用。

注意:default_checkout_countrydefault_checkout_state钩子已经被弃用并替换为WooCommerce 3中的其他内容。

相关:WooCommerce:设置未登录用户默认结帐页面的国家


@SoniaKhan /wp-content/themes/{youractivethemename}/functions.php - imVJ

1
值得注意的是,实现此目的的最新方法可能始终可以在Woocommerce文档中查看,链接在这里
同时需要注意的是,对于现有(已登录)客户来说,删除默认国家可能是不可取的,至少对于允许客户拥有客户账户的网站来说。对于这些客户,地址字段将自动填写,如果您为所有用户删除默认国家,则他们的地址将被填写,但国家将缺失。
考虑到这一点,截至2022年1月,我建议使用以下代码作为实现OP请求的有用方式:
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country', 10, 1 );

function change_default_checkout_country( $country ) {
    // If the user already exists, don't override country
    if ( WC()->customer->get_is_paying_customer() ) {
        return $country;
    }

    return null; 
}

这是对WC文档所传达内容的轻微变化,文档是关于如何更改默认国家的设置。在这个案例中,我们将其设置为Null

0

我想告诉你,我已经解决了同样的问题。

我在这里写了一个小插件:https://github.com/TakesTheBiscuit/woocommerce_set_country

这里的主要用户体验修复是为用户提供一个选择框,让他们在进入购物车或结账之前就可以定制他们的会话体验,例如自定义定价、运费、税费等。

在编写插件的过程中,我还发现管理员(通常作为网站所有者登录)已经经常设置了国家,因此为了测试我们想要排除管理员的默认国家功能。

function change_default_checkout_country($country) {

if (current_user_can( 'manage_options' )) {
    } else {
        if ( WC()->customer->get_is_paying_customer() ) {
            return $country;
        }
    }

    $returnCountry = '';
    if (strlen($_SESSION['wc_country_iso']) == 2) {
        $returnCountry = $_SESSION['wc_country_iso'];
    } else {
        $countries_obj   = new WC_Countries();
        $returnCountry = $countries_obj->get_base_country();
    }

    return $returnCountry;
}

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' , 10, 1 );


我的最终建议是:你会发现运费计算器无法识别这些默认国家,令人沮丧 - 所以你需要想办法解决这个问题。

0
我在functions.php中使用了以下代码,但它根本不起作用,有什么线索吗?
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );

function change_default_checkout_country() {
  return 'UK'; // country code

0

这些功能已被弃用

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );

function change_default_checkout_country() {
    return null;
    //return get_user_meta( get_current_user_id() , 'billing_country', true ); // for retrun user saved country
}

function change_default_checkout_state() {
    return null;
    //return get_user_meta( get_current_user_id() , 'billing_state', true ); // for retrun user saved state
}

已在WooCommerce版本4.5.2中进行测试


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接