Completely new to Woocommerce/wordpress here. On the cart page mydomain.local/cart
What filter should I use to show/hide a flat rate shipping method at certain times. From admin I managed to add an extra flat rate method and named it "Next day". Now I would like to show that flat rate method only before 4PM. I tried in functions.php
add_filter( 'woocommerce_package_rates', 'custom_change_shipping', 10);
function custom_change_shipping($rates) {
var_dump($rates);
}
Nothing seems to change and also I cant seem to debug the $rates
variable as nothing is outputted when var_dump($rates);
. I tried it both anonymous and as an admin but nothing seems to work.
Thanks @LoicTheAztec for confirming whether I was using the right filter. My other problem was that I was not able to output var_dump
on the /cart
page.
I found out that I need to clear my "cart cache" by going to woocommerce->settings->shipping and in the region, disable then enable again and click on save changes. By doing this I could see my output from var_dump
.
Filter posted earlier by @LoicTheAztec (as requested by @robert)
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping', 100 );
function custom_hide_shipping( $rates ) {
$current_time = date_i18n(('H'), current_time('timestamp')); //Uses Timezone Settings > General
$maximum_time = array (
'time' =>'16'); //4PM
if ($current_time >= $maximum['time']){
unset( $rates['flat_rate:X'] ); // change X to your rate id
}
return $rates;
}