Back to Top

How to Verify Government ID with WooCommerce & WordPress?

Updated 5 June 2024

In this dev blog, we’ll learn how to Verify Government ID with WooCommerce and WordPress. In the fast-paced e-commerce world, a secure checkout is vital.

pexels-dom-j-45113

WooCommerce, as you may already know is a popular WordPress plugin that converts a website into an e-commerce store.

Now let us understand how to add a “Verification ID Number” field for government ID verification in WooCommerce and WordPress.

To ensure smoother transactions while collecting essential customer data.

Why Custom Checkout Fields Matter?

Custom checkout fields offer notable advantages in WooCommerce. They enhance data collection for both store owners and customers.

Searching for an experienced
Woocommerce Company ?
Find out More

In this instance, we emphasize the importance of gathering an “Indian Aadhaar card Verification ID Number” for business needs and regulatory adherence.

Creating the Custom Checkout Field

The heart of our implementation lies in adding a custom field to the WooCommerce checkout page. We achieve this through WordPress actions and hooks.

Below is the code snippet that accomplishes this task:

<?php
/**
 * Plugin Name: Verification ID
 * Description: Add government ID verification to WooCommerce.
 * Version: 1.0
 * text-domain:wk-verification
 * Author: Your Name
 */

// Add a custom field to the checkout page

add_action( 'woocommerce_before_checkout_billing_form', 'wk_add_custom_checkout_field' );

function wk_add_custom_checkout_field( $checkout ) {
	$current_user     = wp_get_current_user();
	$saved_license_no = $current_user->license_no;

	woocommerce_form_field(
		'license_no',
		array(
			'type'        => 'text',
			'class'       => array( 'form-row-wide' ),
			'label'       => __( 'Verification Id Number', 'wk-verification' ),
			'placeholder' => 'for india adhaar card id',
			'required'    => true,
			'default'     => $saved_license_no,
		),
		$checkout->get_value( 'license_no' )
	);
}

This code snippet adds the “Verification ID Number” field to the billing section of your WooCommerce checkout page.

It’s important to ensure that you include this code in your WordPress theme’s functions.php file or a custom plugin.

For engaging with customer data, empowering WooCommerce developers to craft and oversee customer accounts within the WooCommerce ecosystem efficiently.

Retrieving User Data to verify government IDs with WooCommerce & WordPress

To enhance the user experience, we retrieve the saved “License Number” from the user’s profile and pre-fill the field.

This saves your customers the hassle of entering the same information repeatedly.

Validation for the Custom Field to Verify Government IDs with WooCommerce

Ensuring that the “Verification ID Number” field is correctly filled out is essential. For this purpose, we’ve implemented a validation function. Here’s the code:

function validate( $posted ) {
    if ( empty( $posted['license_no'] ) ) {
        wc_add_notice( __( 'Please input the License Number.', 'wk-verification' ), 'error' );
    }
}
add_action( 'woocommerce_checkout_process', 'validate' );

This code uses the ‘woocommerce_checkout_process’ action to trigger the validation during the checkout process. If the “Verification ID Number” field is left empty, an error message will be displayed to the customer.

Saving Custom Field Data After a Successful Order

// Save the custom field to user data after a successful order
function save_custom_field( $order_id ) {
	if ( ! empty( $_POST['license_no'] ) ) {
		require_once 'wp-load.php'; // Include WordPress functions.

		$user_id = get_current_user_id();
		update_user_meta( $user_id, 'license_no', sanitize_text_field( $_POST['license_no'] ) );
		// Add a debugging message to check if the function is being called
		error_log( 'User data updated: ' . $user_id . ' - ' . sanitize_text_field( $_POST['license_no'] ) );
	}
}
add_action( 'woocommerce_checkout_process', 'save_custom_field' );

Once you’ve successfully added the “Verification ID Number” field to your WooCommerce store, the next crucial step is to save this data into the user’s profile after a successful order.

This ensures that the collected information is associated with the respective customer. Below is the code and explanation of how to accomplish this:

Conclusion to Verify Government IDs with WooCommerce & WordPress

Incorporating a “Verification ID Number” field into your WooCommerce store can streamline the checkout process, enhance user experience, and help businesses comply with government regulations.

By following this guide and implementing the provided code, you can take a step toward making your store more secure and efficient.

Remember to thoroughly test the implementation and consider additional customizations, such as saving the field to the user’s profile or integrating with third-party services for ID verification.

Call to Action

If you have any questions or need assistance with customizations, feel free to reach out. We invite you to leave your comments and feedback below.

With this how-to guide, you’re well on your way to implementing Government ID Verification with WooCommerce and WordPress.

Elevate your online store’s security and user experience by collecting the necessary identification information seamlessly during checkout.

Support

However, in case of any query or questions regarding the Woocommerce Extensions, you can create a ticket at webkul.uvdesk.com or contact us at store.webkul.com/contacts/ to let us know your views to make the plugin better.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Austin
    • Faraz Ali Khan (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home