避免 WooCommerce Checkout 账单表单覆盖默认 Wordpress 用户数据

Yah*_*sam 3 php wordpress checkout user-data woocommerce

在数据库中,我们有元用户表,其中包含:
和字段first_namelast_name

这些是默认的 WordPress 名字和姓氏。

我们还有:
billing_first_namebilling_last_name

现在,当用户填写帐单表单并继续结帐流程时,Woocommerce 会使用帐单名称字段(默认值)中的新值更新这两个字段。

我尝试过很多使用动作的事情,例如:

woocommerce_before_checkout_billing_form
Run Code Online (Sandbox Code Playgroud)

或者

woocommerce_after_checkout_billing_form
Run Code Online (Sandbox Code Playgroud)

还尝试使用以下方法更新元:

update_user_meta()
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

我希望它不覆盖默认的名字和姓氏,但仅将新值保留在 billing_first_name 和 billing_last_name 中

我认为默认过程是这样的
https://gist.github.com/claudiosanches/ae9a8b496c431bec661b69ef7 ​​3f1a087

请问对此有什么帮助吗?

Loi*_*tec 5

方法是使用挂接在woocommerce_checkout_update_customer动作钩子中的自定义函数:

add_action('woocommerce_checkout_update_customer','custom_checkout_update_customer', 10, 2 );
function custom_checkout_update_customer( $customer, $data ){

    if ( ! is_user_logged_in() || is_admin() ) return;

    // Get the user ID
    $user_id = $customer->get_id();

    // Get the default wordpress first name and last name (if they exist)
    $user_first_name = get_user_meta( $user_id, 'first_name', true );
    $user_last_name = get_user_meta( $user_id, 'last_name', true );

    if( empty( $user_first_name ) || empty( $user_last_name ) ) return;

    // We set the values by defaul worpress ones, before it's saved to DB
    $customer->set_first_name( $user_first_name );
    $customer->set_last_name( $user_last_name );
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并可在 WooCommerce 3+ 中使用