Google Address API Classifying Address as Residential or Commercial Word Press Plugin

The Google Maps API is a software development kit that allows developers to integrate Google Maps functionality into their own websites & applications. The API allows developers to access data from Google Maps & other Google Services, including the Google Maps Geocoding API, which allows developers to convert addresses into geographic coordinates (latitude & longitude), vice versa.

One way that the Google Maps API could be used is to detect whether a given shipping address is a residential or commercial address. This could be done by using the Geocoding API to convert the address into geographic coordinates, & then using the Google Maps API to look up the type of place (e.g., “residential” or “commercial”) associated with those coordinates.

To get a Google Maps API key, you will need to follow these steps:

Go to the Google Cloud Console:

Click the project drop-down & select or create the project for which you want to add an API key.

Click the hamburger menu & select APIs & Services > Credentials.

On the Credentials page, click the Create Credentials button & select API key.

The API key created dialog displays your newly created API key.

Click Close.

The new API key is listed on the Credentials page under API keys.

To restrict the API key for use with Google Maps, do the following:

Click the Edit button for the API key that you want to restrict.

In the Key restriction section, set Application restrictions to HTTP referrers.

In the Referrers field, enter the Referrers. You can enter an asterisk (*) to allow any referrer.

Click Save.

Keep in mind that the Google Maps API is subject to usage limits, & you will need to enable billing with a credit card on your Google Cloud account in order to use the API beyond the free tier. You can view your current usage & limits in the Cloud Console, under the “Usage” tab for the Maps API.

I am assuming you’ve done all the above, next thing you need to do is goto your /wp-content/plugins/, create another directory inside name let say shipping-address-detection/ inside that directory create a file name plugin.php

Paste the following CODE


 * Plugin Name: Shipping Address Detector
 * Description: Detects if a shipping address is residential or commercial and displays it on the order page in the admin area.
 * Version: 1.0
 * Author: Sohaib Khan
 * Author URI:

function wpk_add_meta_box() {
        'wpk_gav', // ID
        'Address Type', // Title
        'wpk_gav_callback', // Callback
        'shop_order', // Post type
        'side', // Context
        'default' // Priority
add_action( 'add_meta_boxes', 'wpk_add_meta_box' );

function wpk_gav_callback( $post ) {
    $order = wc_get_order( $post->ID );
    $shipping_address = $order->get_address( 'shipping' );

    // Check if address is residential or commercial
    $metadata = wpk_get_address_type( $shipping_address );
    $address_type = $metadata->residential ? "Residential" : "Commercial";
    echo '<p>Address Type: <strong>' . esc_html( $address_type ) . '</strong></p>';

function wpk_get_address_type( $address ) {
    // Replace YOUR_API_KEY with your actual API key
    $api_key    = 'YOURAPIKEY';
    $body_param = array(
        'address'   => array(
            'regionCode'    => $address['country'],
            'addressLines'  => $address['address_1'] . " " . $address['city'] ." " . $address['state']  . " " . $address['postcode']

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, '' . $api_key );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $body_param ) );

    $headers = array();
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    $result = json_decode( $result );   
    return $result->result->metadata;

All you need to do is replace $api_key = ‘YOURAPIKEY’; with your own API KEY, activate the plugin and , add some test address it will display if address is Residential and Commercial.

Leave a Reply

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