AI Verified

Name

WooCommerce Registration Form Postcode Field Validator

About

If you mange a store which does not allow for local pickup and you only deliver to specific postcodes, you may wish to save customers the hassle and aggravation of going through the entire checkout process necessarily, simply by asking the user for their postcode when they register. This quick guide adds a custom postcode field to the register form, then compares it against a list of postcodes which you define. If the first two characters of the postcode entered match the list, the user is able to register. If the first two characters of the entered postcode do not match any from the list, an error message will appear and the user is prevented from being able to register.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Simply copy the pre-coded solution to your active child theme’s functions.php or preferably the Code Snippets Plugin.

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://www.ecommercehints.com/woocommerce-registration-form-postcode-field-validator

Codevault

ecommercehints

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://www.ecommercehints.com/

History

Last modified:

14/11/2022

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

WooCommerce Registration Form Postcode Field Validator

 
                    
1/**
2* Snippet Name: WooCommerce registration form postcode field validator
3* Snippet Author: ecommercehints.com
4*/
5 
6// Create the field and display it on the registation form
7add_action('woocommerce_register_form', 'ecommercehints_registraion_form_postcode_field');
8function ecommercehints_registraion_form_postcode_field() {
9 woocommerce_form_field(
10 'billing_postcode',
11 array(
12 'type' => 'text',
13 'required' => true,
14 'label' => 'Your Postcode',
15 'placeholder' => 'Postcode',
16 'description' => 'To check if we deliver to you'
17 ),
18 (isset($_POST['billing_postcode']) ? $_POST['billing_postcode'] : '')
19 );
20}
21 
22// Show an error message if the postcode is empty or not accepted
23add_action('woocommerce_register_post', 'ecommercehints_postcode_field_validation', 10, 3);
24function ecommercehints_postcode_field_validation($username, $email, $errors) {
25 if (empty($_POST['billing_postcode'])) {
26 $errors->add('billing_postcode_error', 'Your postcode is a required field');
27 }
28 $postcode = substr($_POST['billing_postcode'],0,2);
29 $postcodes = array(
30 'DD', 'DA', 'CW', '', ' ', // The list of postcodes accepted
31 );
32 if (!in_array(strtoupper($postcode), $postcodes)) {
33 $errors->add('billing_postcode_error', 'Sorry, you cannot register as we don\'t deliver to you');
34}
35}
36 
37// Save the postcode as user meta
38add_action('woocommerce_created_customer', 'ecommercehints_save_postcode_field');
39function ecommercehints_save_postcode_field($customer_id) {
40 if (isset($_POST['billing_postcode'])) {
41 update_user_meta($customer_id, 'billing_postcode', wc_clean($_POST['billing_postcode']));
42 }
43}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Apply Coupon Before VAT

Added: 1 year ago

Last Updated: 1 year ago

the product price before VAT is retrieved using the "get_price_excluding_tax()" method, and the discount is applied to this price. The discounted price is then set as the product price using the "se...

WooCommerce

AI Verified

2

WooCommerce Change Order Received Title

Added: 1 year ago

Last Updated: 2 months ago

Customising the thank you page title is relatively straight forward and a quick win in improving your Customer Experience (CX). By default, “Order Received” is very generic and actually quite boring....

WooCommerce

AI Verified

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

7

WooCommerce Show Number Of People Viewing Product

Added: 1 year ago

Last Updated: 1 month ago

We want to make this solution absolutely clear from the outset. This snippet does NOT generate the actual number of people viewing the product. It shows a randomly generated number in the range of two...

WooCommerce

AI Verified

5

WooCommerce Show Number Of Units Sold On Product Page

Added: 1 year ago

Last Updated: 2 months ago

Showing the number of product units sold on the single product page is a great way to introduce Fear Of Missing Out (FOMO). You can highlight how many products are sold to let customers know a product...

WooCommerce

AI Verified

3

WooCommerce Add Content Under The Proceed To Checkout Button On The Cart Page

Added: 1 year ago

Last Updated: 2 months ago

How many times have you bought something online and seen trust symbols near the proceed to checkout button on the cart page? “30 Day Money Back Guarantee” or “Secure Checkout” are popular examples. By...