繁体   English   中英

避免使用WooCommerce Checkout Billing表单覆盖默认的Wordpress用户数据

[英]Avoid WooCommerce Checkout Billing form to overwrite default Wordpress user data

在数据库中,我们具有包含以下内容的元用户表:
first_namelast_name字段

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

我们还有:
billing_first_namebilling_last_name

现在,当用户填写帐单表单并进行结帐过程时,Woocommerce使用帐单名称字段(默认值)中的新值来更新两个字段。

我已经尝试了许多使用动作的东西,例如:

woocommerce_before_checkout_billing_form

要么

woocommerce_after_checkout_billing_form

还尝试使用以下命令更新meta:

update_user_meta()

但这不起作用。

我希望它不会覆盖默认的first_name和last_name,但仅将新值保留在billing_first_name和billing_last_name中

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

请问有什么帮助吗?

方法是使用挂在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 );
}

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

经过测试并在WooCommerce中工作3+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM