In Woocommerce, I am trying to remove the 'flat rate' shipping method when the cart total is higher than 500, using the woocommerce_package_rates. But it is not working, when I try to debug it by logging errors in the console, I don't see anything print there either.
add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
function custom_package_rates( $rates, $package ) {
$total = WC()->cart->cart_contents_total;
echo $total;
if( $total > 500 ) {
unset( $rates['flat_rate:2'] );
}
// debug
$log_msgs[]=json_encode($rates);
foreach($log_msgs as $msg) {
echo "<script>console.log($msg);</script>";
}
// return
return $rates;
}