- 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".
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 );