将其他计费注册字段与 WooCommerce 中的默认 Wordpress 字段同步

Adn*_*Ali 4 php wordpress field user-data woocommerce

我已将以下代码添加到 Woocommerce 用户注册表中,以获取注册页面上的账单详细信息。

现在,当新用户注册时,名字和姓氏将在账单详细信息数据库以及默认的 WordPress 用户帐户中注册。如果用户更新其帐户(wordpress 用户帐户)上的名字和姓氏,账单详细信息也应更新。

Woocommerce 设置:

访客结帐已禁用。结帐页面用户注册已启用。登录页面注册已启用。只有注册用户才能进行购买。

  1. 这是用户注册表单,我从结账过程中提取这些额外的账单详细信息。

用户登记表

  1. 在“帐户详细信息”上,我更新了“名字”,它在这里有效,但我在“帐单详细信息”上没有得到相同的更新。如果用户在“帐户详细信息”上更新这两个字段及其电子邮件地址,我希望“帐单详细信息”上有相同的“名字”和“姓氏”更新。

更新“帐户详细信息”中的“名字”和“姓氏”

  1. 现在,在更新“帐户详细信息”上的“名字”和“姓氏”后,我回到“帐单详细信息”,但它仍然显示注册过程中使用的旧“名字”和“姓氏”。另外,我想从账单详细信息中隐藏这 3 个字段:“名字”、“姓氏”和“电子邮件地址”,以免混淆注册用户。我需要数据库中“账单详细信息”的这些更新,只是因为这些信息将打印在发票和电子邮件上

“帐户详细信息”更新后“帐单详细信息”的屏幕截图

仅当管理员或商店经理进入用户配置文件(从后端)并手动按下“更新”按钮时,数据才会同步/更新,然后才会生效。我希望当注册用户从其帐户(前端)进行任何更改时,数据自动同步/更新。

任何帮助将不胜感激。

请检查以下代码:

// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
    $checkout = WC()->checkout;
    foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
        if($key!='billing_email')
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    endforeach;
}

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
    }
}

// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
    foreach ($_POST as $key => $field) :
        // Validation: Required fields
        if( strpos( $key, 'billing_' ) !== false ){
            if($key == 'billing_country' && empty($field) ){
                $validation_errors->add( $key.'_error',  __( 'Please select a country.', 'woocommerce' ));
            }
            if($key == 'billing_first_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );
            }
            if($key == 'billing_last_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );
            }
            if($key == 'billing_address_1' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );
            }
            if($key == 'billing_city' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );
            }
            if($key == 'billing_state' && empty($field) ){
                if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
                    $validation_errors->add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );
            }
            if($key == 'billing_postcode' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );
            }
            /*
            if($key == 'billing_email' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
            }
            */
            if($key == 'billing_phone' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );
            }

        }
    endforeach;
}

add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['required'] = true;
    $fields['billing_country']['required'] = true;
    $fields['billing_address_1']['required'] = true;
    return $fields;
}

// Hidding some billing fields (Wordpress edit user pages)
add_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );
function user_profile_hide_some_fields_css( $user ){
    ?>
    <style>
    .user-edit-php table#fieldset-billing tr:first-child,
    .user-edit-php table#fieldset-billing tr:nth-child(2),
    .user-edit-php table#fieldset-billing tr:last-child {
        display:none;
    }
    </style>
    <?php
}

// Sync hidden billing fields (Wordpress edit user pages)
add_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
add_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
function sync_user_data_wp_and_billing_wc( $user_id )
{
    if( ! empty($_POST['first_name']) ) {
        update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );
    }

    if( ! empty($_POST['last_name']) ) {
        update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );
    }

    if( ! empty($_POST['email']) ) {
        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 6

我稍微重新审视了你的代码,例如最后 4 个函数可以合并为一个,而其他的东西\xe2\x80\xa6

\n
\n

数据更新与同步

\n

现在,当客户在我的帐户页面上更新数据时,所有数据都会由 woocommerce 同步,除了他现有的过去订单\xe2\x80\xa6

\n

如果客户更改结帐字段并处理结帐,则各处数据也会更新\xe2\x80\xa6

\n

因此您无需担心客户同步数据。

\n
\n

注意:挂钩的函数woocommerce_billing_fields将在您的附加注册字段和结帐字段中启用,因为您正在使用结帐对象生成附加注册字段\xe2\x80\xa6您可以使用条件! is_checkout()仅定位我的帐户注册字段。

\n

这是您重新访问的代码:

\n
// Custom function to display the Billing Address form to registration page\nadd_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');\nfunction zk_add_billing_form_to_registration(){\n    $checkout = WC()->checkout;\n    foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :\n        if($key!='billing_email')\n            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );\n    endforeach;\n}\n\n// Custom function to save Usermeta or Billing Address of registered user\nadd_action('woocommerce_created_customer','zk_save_billing_address');\nfunction zk_save_billing_address($user_id){\n    $address = $_POST;\n    foreach ($address as $key => $field){\n        // Only billing fields values\n        if( strpos( $key, 'billing_' ) !== false ){\n            // Condition to add firstname and last name to user meta table\n            if($key == 'billing_first_name' || $key == 'billing_last_name'){\n                $new_key = str_replace( 'billing_', '', $key );\n                update_user_meta( $user_id, $new_key, $_POST[$key] );\n            }\n            update_user_meta( $user_id, $key, $_POST[$key] );\n        }\n    }\n}\n\n// Checking & validation of the additional fields in registration form.\nadd_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );\nfunction zk_validation_billing_address( $username, $email, $validation_errors ){\n    foreach ($_POST as $key => $field) :\n        // Validation: Required fields\n        if( strpos( $key, 'billing_' ) !== false ){\n            if($key == 'billing_country' && empty($field) ){\n                $validation_errors->add( $key.'_error',  __( 'Please select a country.', 'woocommerce' ));\n            }\n            if($key == 'billing_first_name' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );\n            }\n            if($key == 'billing_last_name' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );\n            }\n            if($key == 'billing_address_1' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );\n            }\n            if($key == 'billing_city' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );\n            }\n            if($key == 'billing_state' && empty($field) ){\n                if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)\n                    $validation_errors->add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );\n            }\n            if($key == 'billing_postcode' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );\n            }\n            /*\n            if($key == 'billing_email' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );\n            }\n            */\n            if($key == 'billing_phone' && empty($field) ){\n                $validation_errors->add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );\n            }\n\n        }\n    endforeach;\n}\n\nadd_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );\nfunction sv_required_billing_fields( $fields ) {\n    $fields['billing_phone']['required'] = true;\n    $fields['billing_city']['required'] = true;\n    $fields['billing_country']['required'] = true;\n    $fields['billing_address_1']['required'] = true;\n    return $fields;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

客户无法(永远)访问 WordPress 后端用户编辑页面。只有店长和管理员可以操作\xe2\x80\xa6
\n要在后端同步Wordpress用户数据,您需要选择哪些字段具有优先级:

\n
    \n
  • WordPress 默认字段(或)
  • \n
  • 账单字段(来自 WooCommerce)。
  • \n
\n

最好优先考虑 WordPress 默认字段并隐藏必要的计费字段\xe2\x80\xa6

\n

此代码将隐藏 3 个账单字段(名字、姓氏和电子邮件),并将它们与默认字段更新值同步:

\n
// Hidding some billing fields (Wordpress edit user pages)\nadd_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );\nfunction user_profile_hide_some_fields_css( $user ){\n    ?>\n    <style>\n    .user-edit-php table#fieldset-billing tr:first-child,\n    .user-edit-php table#fieldset-billing tr:nth-child(2),\n    .user-edit-php table#fieldset-billing tr:last-child {\n        display:none;\n    }\n    </style>\n    <?php\n}\n\n// Sync hidden billing fields (Wordpress edit user pages)\nadd_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );\nadd_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );\nfunction sync_user_data_wp_and_billing_wc( $user_id )\n{\n    if( ! empty($_POST['first_name']) ) {\n        update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );\n    }\n\n    if( ! empty($_POST['last_name']) ) {\n        update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );\n    }\n\n    if( ! empty($_POST['email']) ) {\n        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

已测试并有效\xe2\x80\xa6

\n