WP SITES

3085 Coded Tutorials & 288 Plugins

Get Shipping Zone Data for Coding in WooCommerce

These code snippets enable you to get shipping zone data for use with WooCommerce plugin development. You can also use this code in your child themes functions file or code snippets plugin to output shipping zone data on the cart and/or checkout pages.

The code gets :

  • The shipping zone ID
  • The shipping zone name
  • The shipping method id

Zone ID

The 1st code snippet gets the shipping zone ID based on a given shipping method ID.

function get_shipping_zone_by_method_id( $shipping_method_id ) {
    // Load all shipping zones, including the default 'Rest of the World' zone.
    $zones = WC_Shipping_Zones::get_zones();
    $zones[] = WC_Shipping_Zones::get_zone( 0 );

    // Loop through each zone to find the shipping method.
    foreach ( $zones as $zone ) {
        $shipping_methods = $zone['shipping_methods'];
        foreach ( $shipping_methods as $method ) {
            // Construct the full method ID (e.g., 'flat_rate:5').
            $full_method_id = $method->id . ':' . $method->instance_id;
            if ( $full_method_id === $shipping_method_id ) {
                return $zone['zone_id'];
            }
        }
    }

    // Return null if the shipping method ID is not found in any zone.
    return null;
}

You will need to get a shipping method id to use this code correctly. One solution is to use the customer session as you’ll see in that end of this post.

Zone Name

The 2nd snippet enables you to get the shipping zone name based on the zone ID.

function get_shipping_zone_name_by_id( $zone_id ) {
    // Ensure the zone ID is a positive integer.
    if ( ! is_int( $zone_id ) || $zone_id < 0 ) {
        return null;
    }

    // Instantiate the shipping zone object.
    $shipping_zone = new WC_Shipping_Zone( $zone_id );

    // Check if the shipping zone exists.
    if ( $shipping_zone->get_id() === $zone_id ) {
        // Return the name of the shipping zone.
        return $shipping_zone->get_zone_name();
    }

    // Return null if the shipping zone does not exist.
    return null;
}

You will need to use the zone id for the shipping zone.

Method ID

You can get the shipping method ID using this code.

function get_selected_shipping_method_id() {
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');

    if (!empty($chosen_shipping_methods) && is_array($chosen_shipping_methods)) {
        return sanitize_text_field($chosen_shipping_methods[0]); // Return the first selected method
    }

    return null; // Return null if no method is selected
}

Output

You can then use the following code to output the method id, zone id and zone name for debugging purposes like this :

Code or Plugin?

You can install this free plugin to dynamically display this data on shipping method change on your cart page for debugging and development purposes.

You can also use the code in your child theme or custom code snippets plugin in which case you’ll need to change the method to load the jQuery on line 22 from a plugin to get_stylesheet_directory_uri

This code is extended and used in the following plugins :

Leave a Reply

New Plugins