The WooCommerce checkout process is the last step to completing an order. However, fraudulent customers might make fake orders on your online store.
They might use invalid phone numbers, and WooCommerce, by default, does not have a built-in option to validate the Phone number field on the checkout page.
WooCommerce Checkout Phone Number Validation
Additionally, sorting through fake or invalid leads can be time-consuming. To overcome this problem, you need to force validate the WooCommerce checkout process to limit the phone number field to have a ten-digit valid phone number.
If a customer adds a phone number with less than ten digits, then the checkout process will not be completed, and it will show an error message on top of the checkout page.
Steps to Add a Phone Number Validation on the WooCommerce Checkout Page
Here are the steps that you need to follow:
- Log into your WordPress site and access the dashboard as the admin user
- From the dashboard menu, click on the Appearance Menu > Theme Editor Menu. When the theme editor page is opened, look for the theme functions file with the extension functions.php. Open this functions file to add the function to add a phone number validation on the WooCommerce checkout page.
- Add the following line of code to the functions.php file:
// Limit Woocommerce phone field to 10 digits number add_action('woocommerce_checkout_process', 'njengah_custom_checkout_field_process'); function njengah_custom_checkout_field_process() { global $woocommerce; // Check if set, if its not set add an error. This one is only requite for companies if ( ! (preg_match('/^[0-9]{10}$/D', $_POST['billing_phone'] ))){ wc_add_notice( "Incorrect Phone Number! Please enter valid 10 digits phone number" ,'error' ); } }
Instead of my preg_match
, you can check anything else and adjust your conditional code to your needs.
You can also add custom validation for other default fields by checking on the right $_POST
variable or your custom checkout fields after you correctly set them up
Conclusion
In this brief post, you have learned how to add a WooCommerce checkout validation.
This will help you limit fake orders, as customers will be required to enter a valid number. However, you should be careful when editing the functions.php file because you can break your site if you do not know what you are doing.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Count Items Added to Cart WooCommerce Cart Count Code
- How to Get Order ID In Woocommerce
- How to Hide Price If Zero In WooCommerce
- How to Hide Errors From Visitors WooCommerce
- How to Hide the Weight of Product in WooCommerce
- How to Temporarily Disable Checkout In WooCommerce
- How to Set Up WooCommerce You Must be Logged in to Checkout
- How to Remove WooCommerce Payment Options From Checkout
- How to Set Up WooCommerce Checkout Field Placeholder
- How to Add WooCommerce Checkout Default Country
- How to Add WooCommerce VAT Number Checkout Page
- How to Edit WooCommerce Checkout Page Template
- How to Add Date Field to Checkout In WooCommerce
- How to Add WooCommerce Custom Checkout Message
- How to Hide SKU, Category, Tags Meta Product Page WooCommerce
- How to Hide Product Description Heading WooCommerce
- How to Move WooCommerce Coupon Field Checkout Page
- How to Add Hidden Field on WooCommerce Checkout Page
- How to Edit Required WooCommerce Checkout Fields