Hide shipping method in woocommerce if custom checkbox field is selected

Hardii

Well-known member
Trusted Uploader
Jul 1, 2022
146
481
63
Greetings,
So, i'm trying to hide a shipping method when a custom checkout checkbox field is checked. The custom checkbox field is "enabledropshipping" and the custom shipping method i need to hide is "shipping_method_0_flat_rate1".

function conditionaly_hide_shipping_method( $available_methods ) {
$post_data = urldecode($_POST['post_data']);
$post_data_arr = is_string($post_data) ? explode("&", $post_data) : array();
$field_value= '';
foreach ($post_data_arr as $value) {
$value_arr = is_string($value) ? explode("=", $value) : array();
if(isset($value_arr[0]) && $value_arr[0] === 'enabledropshipping'){
$field_value = isset($value_arr[1]) ? $value_arr[1] : '';
}
}
if(is_array($available_methods) && $field_value === 'shipping_method_0_flat_rate1'){
unset( $available_methods['shipping_method_0_flat_rate1'] );
}
return $available_methods;
}
add_filter( 'woocommerce_shipping_methods', 'conditionaly_hide_shipping_method' , 10, 1 );
 

Hardii

Well-known member
Trusted Uploader
Jul 1, 2022
146
481
63
Well thank you sir, i did not think of that :p

I've searched a lot, none of the things i've found worked. Neither with jquery neither with the above function.
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu