0

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

Maybe I can hide them with css the unchecked? enter image description here

netdev
  • 496
  • 1
  • 5
  • 22
  • 1
    Does this answer your question? [Hide specific shipping method for specific products in Woocommerce](https://stackoverflow.com/questions/49552473/hide-specific-shipping-method-for-specific-products-in-woocommerce) – Bhautik Mar 30 '21 at 17:57
  • @Bhautik no because it uses another hook not 'template_redirect' – netdev Mar 30 '21 at 18:00
  • But you can use the `woocommerce_package_rates` filter hook. – Bhautik Mar 30 '21 at 18:01
  • @Bhautik I tried but its not working with my code... I dont know how to combine them – netdev Mar 30 '21 at 18:50

1 Answers1

1

Try this hook woocommerce_before_checkout_shipping_form

add_action( 'woocommerce_before_checkout_shipping_form', 'nhy_chosen_shipping_methods' );

function nhy_chosen_shipping_methods( $checkout ){
    
    $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;
        }
        
    }
    
    // first unset your methods
    WC()->session->__unset( 'flat_rate:8' );
    WC()->session->__unset( 'flat_rate:9' );
    WC()->session->__unset( 'free_shipping:10' );

    // not set based on your conditions.
    if( WC()->cart->cart_contents_total >= 100 ){
        WC()->session->set( 'chosen_shipping_methods', array('free_shipping:10') );
    }elseif( $final_max <= 70 ){
        WC()->session->set( 'chosen_shipping_methods', array('flat_rate:8') );
    }else{ 
        WC()->session->set( 'chosen_shipping_methods', array('flat_rate:9') );
    }
    
}
Bhautik
  • 11,125
  • 3
  • 16
  • 38