WooCommerce Shipping Plugin – Price Adjustment On Shipping Cost Based On Destination Address

This is a business case from a customer using ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label. The shipping cost for a package is calculated based on the weight and dimensions of the package and the destination address. But there are few times when the store owner wants to charge extra or lesser shipping charges based on the country he ships to. The plugin has a price adjustment feature, which applies to all countries that service is applicable to. But if you want to add a price adjustment only to few specific countries, the price adjustment from the plugin settings will not be really applicable since it will apply to all countries. In this article I’ll explain how to use a code snippet to achieve this requirement. The same solution can be used for our other shipping plugins such as:

In this article we’ll take an example of our plugin ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label and set few price adjustments for certain countries. But there are few things you need to set up before adding the code snippet.

Requirements:

Set Up and Configure the ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label

You need to purchase the plugin from our site, and set up the plugin to get the shipping rates. To set up the DHL plugin and retrieve shipping rates, you need to get the API credentials from DHL Express team. You need to open an account and register in https://xmlportal.dhl.com and DHL will provide the Test and Live API credentials. Depending on which set of credentials you received you need to enable/disable the ‘Show DHL Account Rates‘ option under the Rates and services tab in the plugin settings. For Test credentials you need to disable the ‘Show DHL Account Rates‘ option, and for live credentials you need to enable the ‘Show DHL Account Rates’ option.

Make sure all your products (simple and variations) have weight and dimensions in them. You can add weight and dimension while editing the product. Go to shipping tab under the product data and fill up the fields ‘Weight‘,’Length‘,’Width‘ and ‘Height/Thickness‘. The plugin requires these data to calculate shipping and the packing option in the plugin settings. Once done, enable the DHL Express services in the plugin settings under the ‘Rates and Services Tab’ with the product code ‘N’ for Express Domestic, ‘P’ for Express Worldwide, ‘D’ also Express Worldwide (doc type) and U also Express Worldwide (doc type). Different countries require different services, and so depending on the requirement the API will automatically return rates for one of the above services. Now, check if you’re getting the DHL shipping options in the Cart page. If you’re not getting any shipping rates, please make sure you have the address correct or contact our support team.

Lets say we’re shipping to United Kingdom and Australia, and want to add a price adjustment of £5 for United Kingdom, and £10 for Australia. And without the code snippet the shipping costs to those countries show as : 

To United Kingdom, ‘Economy Select’ shows £53.18 and ‘Express World Wide’ Shows £84.74

To Australia, ‘Economy Select’ Shows £40.33 and ‘Express World Wide’ Shows £59.05:

How Can We Add Different Price Adjustment for Different Countries?

Our developers have come with a solution where you can configure a code snippet and add it using the code snippet plugin by Shea Bunge. Its a safe way to add code snippets using the plugin, instead of adding it directly to the functions.php in the theme folder. If you add code snippets in functions.php and there’s some error in the code, your whole site could crash. Hence its wise to use the code snippet plugin which you can download from the WordPress plugin repository –  Download Link. You can install the plugin on your WP plugin page and to add the code go to WordPress menu >Snippets>Add New Snippet. 
Add the below code and click on ‘save and activate’. I’ve added as comments where you need to do changes according to your requirements. You need to replace the ‘UK’ and ‘AU’ country code with the destination country code for which you want to add price adjustment. It should be a two character code, and add a price adjustment value. Enter the shipping ID for which you want to add price adjustment for. Here, I’ve added ‘wf_dhl_shipping’ for DHL shipping.

add_filter( 'woocommerce_package_rates',  'wf_add_charge_if_exceed_cost', 15, 2 );
function wf_add_charge_if_exceed_cost( $available_shipping_methods, $package ){
	
	$methods  = array('wf_dhl_shipping'); //enter the shipping ID for which the price adjustment should apply
	//Config this array with country code, state code and rate to be added.
	$destination_array = array(
		'GB' => array( // define the country code and the price adjustment to add
		     '*' => 5,
		),
		'AU' => array( // define the country code and the price adjustment to add
		    '*' => 10,
		)
	);

	global $woocommerce;
	$customer_country = $woocommerce->customer->get_shipping_country();
	$customer_state    = !empty($destination_array[$customer_country]['*']) ? '*' : $package['destination']['state'];

	foreach($methods as &$current_method) {
		foreach ($available_shipping_methods as $shipping_method => $value) {
			if( strpos( $shipping_method, $current_method ) !== false ) {
				// Cost adjustment
				if ( ! empty( $destination_array[$customer_country][$customer_state] ) ) {
					$value->cost = $value->cost + floatval( $destination_array[$customer_country][$customer_state] );
				}
			}
		}
	}
	return $available_shipping_methods;
}

Now after adding the code snippet you can see the price added to the shipping cost by £5 and £10 for UK and Australia.

To United Kingdom, ‘Economy Select’ shows £58.18 and ‘Express World Wide’ shows £89.74

To Australia, ‘Economy Select’ shows £50.33 and ‘Express World Wide’ shows £69.05:

How will I know the Shipping ID I need to enter?

To get the shipping ID of the services, showing in the cart or checkout page, you can right click the shipping option in the Cart page and select Inspect. And in the console that shows up, the element details will be already selected so you can see the shipping ID under “value”. This you can copy and enter the same in the code snippet in the shipping ID field.

Hope this article have helped you with your case. If you need help setting up the plugin and the code snippet, you can always contact our team.


To explore more details about the shipping plugins, please check out the product pages.

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

    Leave a Reply