View Categories

Change shipping label shown for Cost By Distance Range

< 1 min read

The snippet below shows how you’d go about changing the label for your shipping method(s) based on the range that the customer falls in. PLEASE NOTE; You need to enable the “Display Cost & Unit in Shipping Label” option so to better get this snippet to work. The snippet searches for the range inside the shipping method title which would be present if the option is enabled:

PHP
function sl_cc_change_shipping_range_method_label($rates, $package){

	foreach( $rates as $rate ){
				
		// Ranages and their label.
		$mappings = [
			'0-2.6' => 'Express Delivery (15-30min)',
			'2.6-12' => 'Express Delivery (20-40min)',
			'12-15' => 'Express Delivery (25-50min)',
			'15-20' => 'Express Delivery (30-60min)',
			'20-30' => 'Next Day Delivery with XYZ Courier',
// 			'>= 30' => 'Example label for entry with no end range'
		];
		
		$label = $rate->label;
		
		foreach( $mappings as $range => $preferred_label ){
			
			if( strpos($label, $range) !== false){
				$rate->label = $preferred_label;	
			}
			
		}

	}
	return $rates;
}
add_filter('woocommerce_package_rates', 'sl_cc_change_shipping_method_label', 200, 2);