Installation

On your WordPress dashboard, choose Plugins – Add new.

Search for “WooCommerce Distance Based Fee” and click “Install now” and after that “Activate”.

Google Matrix API key

In order to use the plugin, you need to have a Google Account and you need to generate Google Matrix API key. You also need to have following APIs enabled:

  • Distance Matrix API
  • Places API for Web
  • Google Maps Geocoding API

Instructions how to enable APIs can be found here:
https://support.google.com/googleapi/answer/6158841?hl=en

Instructions to get the API key can be found here: https://developers.google.com/maps/documentation/distance-matrix/get-api-key

Above, view on the API key generation page.

Please note, you also need to have billing enabled in order to use the Google Matrix API. To enable billing, please visit here: https://console.cloud.google.com/project/_/billing/enable

Once you have generated your API key, go to the plugin settings: “Settings – Distance based fee settings” and paste your API key to the “Google API key” field and save settings. After this configure rest of the settings as you wish.

Settings

Choose “Settings – Distance based fee settings” on your WordPress Dashboard to open the plugin settings page.

On the main settings page, you need to enter the Google Matrix API key. After that you can set fee name, divider, price, units and shipping methods which your distance based fee is used for.

Developers

Fee can also be manipulated by dbf_calculated_fee filter hook, which takes 4 parameters: $calculatedFee, $kiloMeters, $divider and $price.

The hook function would look something like this:

function my_dbf_calculated_fee_callback( $calculatedFee, $kiloMeters, $divider, $price ) {
if($kiloMeters < 10) {
// Do something
} else {
// Do something else
}

return $calculatedFee;
}
add_filter( ‘dbf_calculated_fee’, ‘my_dbf_calculated_fee_callback’, 10, 4 );

You can also manipulate the starting address, e.g. in case you want the distance to be calculated on different starting points. Hooks are: dbf_origin_city_filter, dbf_origin_zip_filter and dbf_origin_address_filter.

The hook function would look something like this:

function my_dbf_calculated_fee_callback( $originAddress ) {

$originAddress = ”1234 Fake Street;

return $originAddress;
}
add_filter( ‘dbf_calculated_fee’, ‘my_dbf_calculated_fee_callback’, 10, 1 );