How to process WooCommerce Shipping for Countries with no ZIP code? (Code Snippet)

There are many countries that don’t have states or an official postal code. The WooCommerce cart’s shipping calculator only provides Country, State, and Postal code field by default. While the recent versions of WooCommerce show the city name field when a country with no postal code is selected, some of the older versions do not show the city name field, hence making difficult for customers to check out.

Some of the WooCommerce versions make the postal code mandatory, so how would you calculate shipping for the countries that don’t have a postal code?

ELEX has a simple, effective solution to provide a text field for accepting city names when the selected country does not have a postal code. This city text field can be added to your WooCommerce cart, by using our City for country plugin. You can download the free plugin and install on your WordPress site.

A new field accepting city names will appear in your WooCommerce cart page, as shown in the screenshot below.

Once the plugin is installed, your WooCommerce cart should be ready to accept city names for processing the shipments.

For this, you need to add the following code snippet in the functions.php file of your currently activated theme.

function solution_to_countries_with_no_zipcodes( $package_destination_country, $package_destination_postcode ) {

    $countries_with_no_postcodes = array('AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK', 'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR', 'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW');

    if(in_array($package_destination_country, $countries_with_no_postcodes)){
        add_filter( 'woocommerce_default_address_fields' , 'skip_postcode_validation' );
        return 0;
    }

    return $package_destination_postcode;
}

function skip_postcode_validation( $address_fields ) {
  $address_fields['postcode']['required'] = false;
  return $address_fields;
}

With this solution, the postal code field will no longer be a mandatory field and you should be able to create shipment or calculate shipping cost without any issues.

 


Explore our blog section for more related articles.

You can also check out WooCommerce and WordPress plugins in ELEX.

    Leave a Reply