• Resolved huzaifa0300

    (@huzaifa0300)


    Hello WooCommerce Support,

    I have customized my WooCommerce checkout form to make the email field optional using the following PHP code:

    add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' ); function custom_woocommerce_billing_fields( $fields ) { $fields['billing_email']['required'] = false; return $fields; }

    The email field is now optional, but I’ve encountered an issue where orders from repeated customers are being treated as new orders, even if the phone number is the same. Currently, user identification is done via email, but since it is optional, I would like WooCommerce to use the phone number for user identification instead of the email.

    Is it possible to implement user identification via phone number in WooCommerce? If so, could you guide me on how to achieve this or provide custom code for the same?

    Looking forward to your assistance.

    Thank you!
    Huzaifa Qureshi

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @huzaifa0300

    While WooCommerce primarily uses email for user identification, it is possible to modify this to use phone numbers instead. To do so, I recommend using a plugin that allows you to use phone numbers for user identification, such as the Digits: WordPress Mobile Number Signup and Login.

    For reference, this particular forum is meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter huzaifa0300

    (@huzaifa0300)

    Hi Shameen,

    Thank you for your response and suggestion to use the “Digits: WordPress Mobile Number Signup and Login” plugin. However, I don’t intend to use SMS verification or OTP functionality. My goal is simpler:

    I want to keep the email field optional during checkout, but I’m facing an issue where, when a customer doesn’t provide an email, each order is treated as a new one, even for returning customers. This breaks the ability to track customer data.

    What I need is for user backend identification to be done via phone number instead of email, so that even when the email field is optional or left blank, returning customers are recognized by their phone number, and their previous data (such as order history) is maintained.

    Is there any solution or custom code to allow this kind of phone number-based identification without relying on SMS or OTPs?

    Looking forward to your guidance!

    Thank you,
    Huzaifa Qureshi

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @huzaifa0300

    Is there any solution or custom code to allow this kind of phone number-based identification without relying on SMS or OTPs?

    You can use a checkout field editor plugin to make the email field optional, such as the Checkout Field Editor for WooCommerce or Checkout Manager for WooCommerce.

    Please note that writing or providing custom code is not within the scope of our support policy. If you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter huzaifa0300

    (@huzaifa0300)

    Hi WooCommerce Support Team,

    Thank you for your response. I wanted to let you know that I have successfully resolved the issue with making the email field optional during checkout using custom code. Here’s a brief overview of how it works:

    Custom PHP Code Implementation:

    // Make email field optional add_filter( 'woocommerce_billing_fields', 'make_email_field_optional' ); function make_email_field_optional( $fields ) { $fields['billing_email']['required'] = false; return $fields; } // Identify and log in user by phone number or email add_action( 'woocommerce_before_checkout_process', 'identify_and_log_in_user_by_phone_number_or_email' ); function identify_and_log_in_user_by_phone_number_or_email() { $phone_number = $_POST['billing_phone']; $email = $_POST['billing_email']; // Check if phone number is provided if ( ! empty( $phone_number ) ) { $user_query = new WP_User_Query( array( 'meta_query' => array( array( 'key' => 'billing_phone', 'value' => $phone_number, 'compare' => '=' ) ) ) ); $users = $user_query->get_results(); if ( $users ) { $user = $users[0]; wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID ); do_action( 'wp_login', $user->user_login, $user ); } else { // Create a new user if phone number is not found $user_data = array( 'user_login' => $phone_number, 'role' => 'customer' ); if ( ! empty( $email ) ) { $user_data['user_email'] = $email; } $user_id = wp_insert_user( $user_data ); if ( $user_id ) { update_user_meta( $user_id, 'billing_phone', $phone_number ); if ( ! empty( $email ) ) { update_user_meta( $user_id, 'billing_email', $email ); } wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id ); do_action( 'wp_login', $user_data['user_login'], $user ); } } } // Check if email is provided if ( ! empty( $email ) ) { $user_query = new WP_User_Query( array( 'meta_query' => array( array( 'key' => 'user_email', 'value' => $email, 'compare' => '=' ) ) ) ); $users = $user_query->get_results(); if ( $users ) { $user = $users[0]; wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID ); do_action( 'wp_login', $user->user_login, $user ); } } // Display error messages if phone number and email are empty if ( empty( $phone_number ) && empty( $email ) ) { wc_add_notice( __( 'Please enter your phone number or email address.', 'text-domain' ), 'error' ); } }

    Advantages of Custom Code:

    • Consistent User Identification: Unlike using checkout field editor plugins which might treat every customer as a new customer (due to the lack of record consistency), this code ensures that returning customers are identified properly, whether they use a phone number or email.
    • Improved User Experience: The system logs in returning users automatically based on their phone number or email, creating a seamless checkout experience.
    • Flexibility: Users can check out with either a phone number or email, providing a more flexible approach.

    For anyone needing similar functionality, they can use this custom code. If there are any issues or additional support is needed, please feel free to contact me through my website: Huzaifa Qureshi.

    Thank you!

    Best regards,
    Huzaifa Qureshi

    Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hi @huzaifa0300,

    We appreciate your proactive approach and the detailed explanation of your PHP code implementation.

    Your solution offers a great way to improve user experience by allowing customers to check out using either their phone number or email. It also ensures returning customers are identified properly, which can definitely help in maintaining customer records consistency.

    Your contribution to the WooCommerce community is invaluable. Sharing solutions like this can greatly benefit others who may be facing similar challenges.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.