如何在 WooCommerce 的注册页面上创建密码和确认密码

时间:2021-01-28 12:25:45

标签: php wordpress woocommerce plugins themes

我正在学习 WooCommerce 开发。我在注册页面上,输入电子邮件并单击按钮后,我将通过电子邮件获取密码。

现在我的问题是,我不想要密码的电子邮件。我已经使用密码创建了注册页面并确认了密码字段。

我希望可以在那里输入自己的密码,而不是通过电子邮件获取此密码。

function woocom_extra_register_fields() {
    ?>
    <p class="form-row form-row-wide mb-4">
        <label for="reg_billing_password" class="text-uppercase"><?php _e('Password', 'woocommerce' ); ?><span class="required">*</span></label><input type="password" class="input-text form-control" name="billing_password" id="reg_billing_password" value="<?php if ( ! empty( $_POST['billing_password'] ) ) esc_attr_e( $_POST['billing_password'] ); ?>" placeholder="Password" />
    </p>
    
    <p class="form-row form-row-wide">
        <label for="reg_billing_confirmpassword" class="text-uppercase"><?php _e('Confirm Password', 'woocommerce'); ?><span class="required">*</span></label><input type="password" class="input-text form-control" name="billing_confirmpassword" id="reg_billing_confirmpassword" value="<?php if ( ! empty( $_POST['billing_confirmpassword'] ) ) esc_attr_e( $_POST['billing_confirmpassword'] ); ?>" placeholder="Confirm Password"/>
    </p>
    <?php
}
    
add_action('woocommerce_register_form', 'woocom_extra_register_fields');
    
function woocom_validate_extra_register_fields( $username, $email, $validation_errors )
{
    if (isset($_POST['billing_password']) && empty($_POST['billing_password']) ) 
    {
        $validation_errors->add('billing_password_error', __('Password is required!', 'woocommerce'));
    }
    
    if (isset($_POST['billing_confirmpassword']) && empty($_POST['billing_confirmpassword']) ) 
    {
        $validation_errors->add('billing_confirmpassword_error', __('Confirm password is required!', 'woocommerce'));
    }
    return $validation_errors;
}
    
add_action('woocommerce_register_post', 'woocom_validate_extra_register_fields', 10, 3);
    
function woocom_save_extra_register_fields($customer_id) {
    
    if (isset($_POST['billing_password'])) 
    {
        update_user_meta($customer_id, 'billing_password', sanitize_text_field($_POST['billing_password']));
    }
    
    if (isset($_POST['billing_confirmpassword'])) 
    {
        update_user_meta($customer_id, 'billing_confirmpassword', sanitize_text_field($_POST['billing_confirmpassword']));
    }
}
    
add_action('woocommerce_created_customer', 'woocom_save_extra_register_fields');

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

要在创建新帐户时禁用自动生成密码,您必须从 WooCommerce 禁用创建帐户时,自动生成帐户密码选项管理页面。

为此,请转至 WooCommerce > 设置 > 帐户和隐私

disable automatic generation of the account password

您可以在此处找到更多信息:WooCommerce - Accounts & Privacy Settings

相关问题