Iam trying to set a shipping method based on product selection, I have achieve that but I want also to hide the other shipping methods so the user cannot see them and cannot select them. This is my code where the shipping method is selected correctly:
add_action( 'template_redirect', 'nhy_chosen_shipping_methods' );
function nhy_chosen_shipping_methods(){
remove_action(current_filter(), __FUNCTION__);
$items = WC()->cart->get_cart();
$max_dimension = 0;
$final_max = 0;
foreach( $items as $item => $values ){
$_product = wc_get_product( $values['data']->get_id());
if(empty($values['we_custom_dimensions'])){
$max_dimension = $_product->get_length() >= $_product->get_height() ? $_product->get_length() : $_product->get_height();
}
else{
$dims = explode('x', $values['we_custom_dimensions']);
$max_dimension = $dims[0] >= $dims[1] ? $dims[0] : $dims[1];
}
if($max_dimension > $final_max){
$final_max = $max_dimension;
}
}
if( WC()->cart->cart_contents_total >= 100 ){
WC()->session->set( 'chosen_shipping_methods', array('free_shipping:10') );
return;
}
if($final_max <= 70){
WC()->session->set( 'chosen_shipping_methods', array('flat_rate:8') );
return;
}
else{
WC()->session->set( 'chosen_shipping_methods', array('flat_rate:9') );
return;
}
}
UPDATE