Woocommerce Shipping – Set Minimum Shipping Cost

Here’s a code snippet to set minimum shipping cost to the shipping methods. The reason we are providing this is because some of the shipping options are only few cents such as the First Class letter rates and post card rates are just about $0.40. However, in different scenarios,  several shop owners wanted to charge a minimum of $3 per shipping option.

You can add the below code snippet to the functions.php in the theme folder and it will charge a minimum of the value given in the code snippet.

add_filter( 'woocommerce_package_rates', function( $shipping_costs) {
	
	$shipping_method_min_cost = array(
		'wf_shipping_usps:D_FIRST_CLASS'	=>	3,	// Shipping id 	=> min_cost
		'wf_shipping_usps:D_PRIORITY_MAIL'	=>	10.20,
		'wf_shipping_usps:D_EXPRESS_MAIL'	=>	30,
	);

	foreach( $shipping_costs as $shipping_cost ) {
		$shipping_method_id = $shipping_cost->get_id();
		if( isset($shipping_method_min_cost[$shipping_method_id]) ) {
			$cost = (float) $shipping_cost->get_cost();
			if( $cost < $shipping_method_min_cost[$shipping_method_id] ) { $shipping_cost->set_cost($shipping_method_min_cost[$shipping_method_id]);
			}
		}
	}
	return $shipping_costs;
});

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 Show Hide shipping code snippet in the shipping ID field.
Now before adding the code snippet the cart shows the original shipping cost. 
After adding the code snippet: 

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

    Leave a Reply