I am creating custom woocommerce register page. A customer can create an account using a name, an email and a mobile number. All three fields are required.
Here is my code:
add_action('init', 'devsol_customer_auth_register');
function devsol_customer_auth_register() {
$customer_name = sanitize_text_field( $_POST['reg_billing_name'] );
$customer_phone = sanitize_text_field( $_POST['reg_billing_phone'] );
$customer_email = sanitize_email( $_POST['reg_billing_email'] );
$customer_password = wp_generate_password( 12, false );
$noemail = '';
if (isset( $_POST['reg_billing_name'] ) && empty( $_POST['reg_billing_name'])) {
$GLOBALS['ERROR'] = 'register-name';
}
else if (isset( $_POST['reg_billing_email'] ) && empty( $_POST['reg_billing_email'])) {
$GLOBALS['ERROR'] = 'register-email';
}
else if (email_exists($customer_email)) {
$GLOBALS['ERROR'] = 'register-email-exist';
}
else if (isset( $_POST['reg_billing_phone'] ) && empty( $_POST['reg_billing_phone'])) {
$GLOBALS['ERROR'] = 'register-phone';
}
else if ((!preg_match('/^[0-9]{11}$/D', $_POST['reg_billing_phone'] )) && !empty( $_POST['reg_billing_phone'])) {
$GLOBALS['ERROR'] = 'register-phone-validation';
}
else if (!username_exists($customer_phone)) {
$user_id = wp_create_user($customer_phone, $customer_password, $noemail);
if ( !is_wp_error( $user_id ) ) {
if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $user_id ) ) {
wc_set_customer_auth_cookie( $user_id );
}
update_user_meta($user_id, 'billing_first_name', $customer_name);
update_user_meta($user_id, 'billing_email', $customer_email);
update_user_meta($user_id, 'billing_phone', $customer_phone);
wp_update_user (array(
'ID' => $user_id,
'first_name' => $customer_name,
'display_name' => $customer_name,
'nickname' => $customer_name,
'role' => 'customer', ));
}
}
else {
$GLOBALS['ERROR'] = 'duplicate-register';
}
}
I am getting this error on every page
Notice: Undefined index: reg_billing_name in /Applications/AMPPS/www/flatsome/wp-content/plugins/devsol-auth-customer/index.php on line 110
Notice: Undefined index: reg_billing_phone in /Applications/AMPPS/www/flatsome/wp-content/plugins/devsol-auth-customer/index.php on line 111
Notice: Undefined index: reg_billing_email in /Applications/AMPPS/www/flatsome/wp-content/plugins/devsol-auth-customer/index.php on line 112
Notice: Undefined index: reg_billing_phone in /Applications/AMPPS/www/flatsome/wp-content/plugins/devsol-auth-customer/index.php on line 131
Please help.
You're seeing that notification everywhere because the devsol_customer_auth_register
function is hooked into init
which fires every time WordPress loads. There's likely a better hook to use.
add_action('init', 'devsol_customer_auth_register');
Secondly, The notification are coming from these lines, because the $_POST variable don't exist at the time when variables are being set to equal them.
$customer_name = sanitize_text_field( $_POST['reg_billing_name'] );
$customer_phone = sanitize_text_field( $_POST['reg_billing_phone'] );
$customer_email = sanitize_email( $_POST['reg_billing_email'] );
To avoid this, check if the variables exist first.
$customer_name = null;
$customer_phone = null;
$customer_email = null;
if ( isset( $_POST['reg_billing_name'] ) ) {
$customer_name = sanitize_text_field( $_POST['reg_billing_name'] );
}
if ( isset( $_POST['reg_billing_phone'] ) ) {
$customer_name = sanitize_text_field( $_POST['reg_billing_phone'] );
}
if ( isset( $_POST['reg_billing_email'] ) ) {
$customer_name = sanitize_text_field( $_POST['reg_billing_email'] );
}
And lastly, here's a snippet that should take care of adding those extra profile fields
function woocommerce_edit_my_account_page() {
return apply_filters( 'woocommerce_forms_field', array(
'woocommerce_reg_billing_name' => array(
'type' => 'text',
'label' => __( 'Reg Billing Name', 'textdomain' ),
'placeholder' => __( 'Billing Name', 'textdomain' ),
'required' => false,
),
'woocommerce_reg_billing_phone' => array(
'type' => 'text',
'label' => __( 'Reg Billing Phone', 'textdomain' ),
'placeholder' => __( 'Billing Phone', 'textdomain' ),
'required' => false,
),
'woocommerce_reg_billing_email' => array(
'type' => 'text',
'label' => __( 'Reg Billing Email', 'textdomain' ),
'placeholder' => __( 'Billing Email', 'textdomain' ),
'required' => false,
),
) );
}
function edit_my_account_page_woocommerce() {
$fields = woocommerce_edit_my_account_page();
foreach ( $fields as $key => $field_args ) {
woocommerce_form_field( $key, $field_args );
}
}
add_action( 'woocommerce_register_form', 'edit_my_account_page_woocommerce', 15 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.