A
A
ashfedor2019-11-29 12:51:14
User identification
ashfedor, 2019-11-29 12:51:14

In Woocomerce, the user has a phone field. and how to withdraw it for registration do not tell me?

Who knows how to connect the phone field to the registration form in Wocomerce.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ashfedor, 2019-11-29
@ashfedor

Found a solution in the open spaces. Implemented it seems to work well!
## --- FOR CHECKOUT --- ##
// Checkout billing phone validation (Checking length)
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
if ( $_POST['billing_phone'] && strlen($_POST['billing_phone']) < 10 )
wc_add_notice( __('Please type a correct phone number…', 'woocommerce'), 'error' );
}
## --- FOR CUSTOM REGISTRATION FORM --- ##
// Add custom fields to registration form.
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
<?php _e('First name', 'woocommerce'); ?>*
<?php _e('Last name', 'woocommerce'); ?>*
<?php _e( 'Phone', 'woocommerce' ); ?>
*
<?php
}
// Checking & validation of custom fields in registration form.
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
function text_domain_woo_validate_reg_form_fields( $username, $email, $validation_errors ) {
if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
$validation_errors->add('billing_first_name_error', __( 'İsim alanı zorunludur! / Name field is required!', 'woocommerce'));
$validation_errors->add('billing_last_name_error', __('Soyisim alanı zorunludur! / Surname field is required!', 'woocommerce'));
}
if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) {
$validation_errors->add('billing_phone_error', __('Telefon alanı zorunludur! / Phone field is required!' , 'woocommerce'));
}
// ==> CHECKING PHONE LENGTH (10 character minimal) <==
if (isset($_POST['billing_phone']) && strlen($_POST['billing_phone']) < 10 ) {
$validation_errors->add( 'billing_phone_error', __('Please type a correct phone number…', 'woocommerce'));
}
return $validation_errors;
add_action( 'woocommerce_created_customer', 'custom_save_extra_register_fields' ); // <==== Missing
function custom_save_extra_register_fields($customer_id) {
//First name field
if (isset($_POST['billing_first_name'])) {
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name ']));
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
//Phone field
if (isset($_POST['billing_phone'])) {
update_user_meta($customer_id, 'phone', sanitize_text_field($_POST['billing_phone']));
update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']));
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question