Although the function seems to work perfectly on the front end, on the backend I have :
CRITICAL Uncaught Error: Call to a member function get_cart_contents_weight() on null in........
This is the function.
add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 10, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways ) {
if ( is_admin() )
return $available_gateways;
$total_weight = WC()->cart->get_cart_contents_weight();
if ( $total_weight >= 2000 && isset ($available_gateways['cod']) )
unset($available_gateways['cod']); // unset 'cod'
return $available_gateways;
}