This topic has 4 replies, 2 voices, and was last updated 1 years, 2 months ago ago by Tony Rodriguez
I would like to require users to provide their cellphone numbers upon registration, since it would be useful to get in touch with them when it comes to their order details and tracking. But while I have found some code examples, I have not been able to make them work.
This one doesn’t seem to do anything
https://njengah.com/woocommerce-make-phone-number-required/?expand_article=1 IIRC,
and this one
https://www.cloudways.com/blog/add-woocommerce-registration-form-fields/
seems to mess about with other fields and from what I can tell doesn’t check that the user inputs only numbers when it comes to the mobile phone field and also gives me an error at line 34 after I activated it.
This one also doesn’t seem to check for incorrect input (aka preventing letters from being entered, but I could be wrong),
https://webkul.com/blog/custom-registration-form-fields-in-woocommerce/
Hello, @thehawkman,
We regret the inconvenience you have experienced due to the issue on your website. However, we would like to clarify that the problem is not related to our theme.
It appears that the custom code you have implemented on your site is not functioning as expected. We recommend that you remove the code that you have added to your site, which is causing an error at line #34.
For further assistance, we suggest you refer to the instructions provided in the following thread: https://www.8theme.com/topic/registration-with-phone-number-on-woocommerce-my-account/. We believe this resource may guide you toward resolving the issue.
Currently, the error is visible on the main page https://dev.orinoco.ro/my-account/. As this page is part of WooCommerce, the issue may not be directly related to the theme activated on your site, suggesting a potential problem on WooCommerce’s end.
Best Regards,
8Theme’s Team
For further assistance, we suggest you refer to the instructions provided in the following thread: https://www.8theme.com/topic/registration-with-phone-number-on-woocommerce-my-account/. We believe this resource may guide you toward resolving the issue.
Currently, the error is visible on the main page https://dev.orinoco.ro/my-account/. As this page is part of WooCommerce, the issue may not be directly related to the theme activated on your site, suggesting a potential problem on WooCommerce’s end.
There is no problem with WC it is just that the code for adding a mobile phone (which I was testing via a plugin) adds new fields to the form but it had some problems. The code is literally from the same page mentioned in the thread you linked to, as luck would have it.
I figured out what was causing the error so it’s gone now and submission works and is saved, but there is one small problem with the code as in it only checks if the phone number is filled, but does not check for user inputting letters instead of numbers.
<?php
/**
* Plugin Name: Trickster Add mobile number to Woocommerce registration form
* Plugin URI: https://www.orinoco.ro/
* Description: Add mobile number to Woocommerce registration form
* Version: 0.1
* Author: cloudways/Trickster
* Author URI: https://www.cloudways.com/
**/
/**
* https://www.cloudways.com/blog/add-woocommerce-registration-form-fields/
* 1. enable registration in admin
* 2. Add Custom Code in Functions.php File
*/
function wooc_extra_register_fields() {?>
<p class="form-row form-row-wide">
<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* 3. Validate Registration Form Fields
*/
/**
* register fields Validating.
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* 4: Save the Values into the Database
*/
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_phone'] ) ) {
// Phone input filed which is used in WooCommerce
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['billing_first_name'] ) ) {
//First name field which is by default
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// First name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// Last name field which is by default
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
Perhaps you would be so kind as to help out with that?
I know you can do it like this
if ( ! (preg_match('/^[0-9]{10}$/D', $_POST['billing_phone'] ))){
wc_add_notice( "Incorrect Phone Number! Please enter valid 10 digits phone number" ,'error' );
but I don’t knpw how to “plug” that function n the code above.
Hello, @thehawkman,
We would like to inform you that we are prepared to provide support that falls within our designated tasks – https://www.8theme.com/documentation/xstore/support/support/ .
However, we are unable to provide and dedicate time to client customization as it is beyond our responsibilities.
If you would like to pursue paid customization services, we recommend submitting a customization request to the Codeable team via their website: https://www.codeable.io/?ref=qGTdX
Thanks for your understanding.
Best Regards,
8Theme’s Team
You must be logged in to reply to this topic.Log in/Sign up