Make last name field unique for guests on WooCommerce checkout

I would like to make the entered values in last name field in checkout page unique or validate for duplicates.

This is for guests users only where they fill the first name and last name. I currently have this snippet but this does not have the desired result. Any advice?

add_action( 'woocommerce_before_checkout_billing_form', 'lastname_field' );

function validate_lastname_field($exist_error )  {
    global $wpdb;
    $billing_last_name = $_POST['billing_last_name'];
    $results = $wpdb->get_results('SELECT * FROM `abc_usermeta` where meta_key = "billing_last_name" AND meta_value = "'.$billing_last_name.'"');
    if ( $results ) {
        $exist_error->add( 'billing_last_name_error', __( 'Last name already exists.', 'woocommerce' ) );

    return $exist_error;


Your code contains some mistakes

  • Use woocommerce_after_checkout_validation action hook for validation
  • Your code is vulnerable to SQL injection, use prepared statements
  • $wpdb: prefix is giving the table prefix of the site. So generating table name dynamically through this will help to keep the query correct even on many sites with different table prefixes
  • Your code will be executed for both guests and logged in users

So you get:

function action_woocommerce_after_checkout_validation( $data, $error ) {
    // Only for guests
    if ( is_user_logged_in() ) return;
    global $wpdb;

    // Billing last name
    $billing_last_name = $data['billing_last_name'];
    // Executes a SQL query and returns the entire SQL result.
    $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}usermeta WHERE meta_key = 'billing_last_name' AND meta_value = %s", $billing_last_name ) );

    // NOT empty
    if ( $results ) {
        $error->add( 'validation', __( 'Last name already exists.', 'woocommerce' ) );
add_action( 'woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );
Tags: PHP / SQL / Wordpress

Similar questions

Pull woocommerce checkout first name field value on checkout submit
Desired outcome Looking to to pull the first name field value on checkout submit The referenced snippet below is in form-checkout.php template before the start of the checkout form. For some reason the checkout submit button is not responding to woocommerce_checkout_place_order when I create a simple form with new field and a test button it works v...
SQL to set Display Name to First Name + Last Name
I'm trying to set the wp_users.display_name for a rather larger users db using SQL query. Got till this point: But trying to change the first SELECT statement to an UPDATE doesn't work:
How to make iframe previews unique to each post. My previews are all showing the same content of the last post on the page
I'm trying to debug an issue with my code. Any help would be much appreciated. Each post on my wordpress site has a span that when focused on through the :focus CSS, reveals an iframe popup which is a preview of a link in the post which has a Unique ID equivalent to the Post ID. Example of the links from the Admin Dashboard for posting on Wordpress...
Add new user : make the fields First Name and Last name required
I'm searching how to make First Name and Last name fields required when we add a new user. Right now only username and Email fields are required. I found a way by adding class="form-required" for the first and last name fields on the file user-new.php. But I'm looking for a method with adding code on function.php and not touch to the Word...
Add a checkout hidden field with a random unique string value in woocommerce
In Woocommerce, I would like to add a hidden field to in checkout page, which attribute value will be a custom generated unique random string of character. Is that possible?
Change WooCommerce checkout city field to a dropdown for a unique country
In Woocommerce, I wrote some code for billing and shipping city to use the shipping charge. Now I want to change the cities list when change country. This is my custom code: But when the user change the country from Saudi Arabia to Qatar, the billing_city does not change to text type, so we must refresh the page to show billing_city as text type. I...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.