skip to Main Content

Hide Shipping Method Based on Weight

The code below will remove a specified shipping option, in this case one that was generated from the Table Rate Shipping method for WooCommerce. In this scenario, the admin wishes to cap the customer’s order to 30kg and not allow this shipping option to be returned if it exceeds that.

add_filter( 'woocommerce_package_rates', 'hide_shipping_weight_based', 10, 2 );
function hide_shipping_weight_based( $rates, $package ) {
	// Set weight variable
	$cart_weight = 0;

	// Shipping rate to be excluded
	$shipping_id = 'table_rate_shipping_first-class';

	// Calculate cart's total
	foreach( WC()->cart->cart_contents as $key => $value)
		$cart_weight += $value['data']->weight * $value['quantity'];

	if( $cart_weight <= 30 )
		unset( $rates[ $shipping_id ] );

	return $rates;
}

Many shipping methods, including the ones that come packaged in WooCommerce, only return one shipping rate, so the shipping rate ID is often the shipping method ID. In this example, written for the Bolder Elements Table Rate method, it is the shipping method ID followed by an underscore and the value under the Identifier column for the rate that is to be removed.

  • Was this article helpful ?
  • yesno
1 out of 1 visitors found this article helpful
Views: 1111
Back To Top