代码之家  ›  专栏  ›  技术社区  ›  Yahya Essam

避免使用WooCommerce签出账单表单覆盖默认Wordpress用户数据

  •  1
  • Yahya Essam  · 技术社区  · 7 年前

    在我们的数据库中
    这个 first_name last_name

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

    我们还有:
    billing_first_name billing_last_name .

    (默认值)。

    我尝试过很多动作,比如:

    woocommerce_before_checkout_billing_form
    

    woocommerce_after_checkout_billing_form
    

    还尝试使用以下内容更新meta:

    update_user_meta()
    

    但它不起作用。


    我认为默认过程是这样的
    https://gist.github.com/claudiosanches/ae9a8b496c431bec661b69ef73f1a087

    有什么帮助吗?

    1 回复  |  直到 7 年前
        1
  •  4
  •   LoicTheAztec    7 年前

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

    代码进入函数。活动子主题(或主题)或任何插件文件中的php文件。