title: Hide shipping method based on the radio button in WooCommerce checkout page
body: <p>I put a radio button in the checkout page with the code below.</p>
  
  <pre><code>add_action( 'woocommerce_before_checkout_shipping_form', 'custom_shipping_radio_button', 10, 1 );
  function custom_shipping_radio_button( $checkout ) {
  
      woocommerce_form_field( 'shipping_type', array(
          'type' =&gt; 'radio',
          'class' =&gt; array( 'form-row-wide' ),
          'label' =&gt; __('收件方式 *'),
          'options' =&gt; array(
              'shipping_1' =&gt; __('全家店到店'),
              'shipping_2' =&gt; __('指定地址'),
              'shipping_3' =&gt; __('自行取貨'),
          ),
      ), $checkout-&gt;get_value( 'shipping_type' ) );
  }
  </code></pre>
  
  <p>I want to hide the option based on shipping method. For example, if the customers choose local pickup, options, shipping_1 and shipping_2 will disappear. I searched some information and tried to make the codes as below.</p>
  
  <pre><code>add_action( 'woocommerce_after_checkout_form', 'hide_shipping_type' );
  function hide_shipping_type( $available_gateways ) {
  global $woocommerce;
  
      $chosen_methods = WC()-&gt;session-&gt;get( 'chosen_shipping_methods' );
      $chosen_shipping_no_ajax = $chosen_methods[0];
          if ( 0 === strpos( $chosen_shipping_no_ajax, 'local_pickup' ) ) {
              ?&gt;
                  &lt;script type="text/javascript"&gt;
                      jQuery('#shipping_type_shipping_1,#shipping_type_shipping_2').fadeOut();
                  &lt;/script&gt;
              &lt;?php
      }
  
      ?&gt;
          &lt;script type="text/javascript"&gt;
              jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
                  var val = jQuery( this ).val();
                      if (val.match("^local_pickup")) {
                          jQuery('#shipping_type_shipping_1,#shipping_type_shipping_2').fadeOut();
                      } else {
                      jQuery('#shipping_type_shipping_1,#shipping_type_shipping_2').fadeIn();
                      }
                  });
          &lt;/script&gt;
      &lt;?php
  }
  </code></pre>
  
  <p>I found that the labels for the options cannot be hidden. I thought the problem may be caused by jQuery script. However, I cannot make it better.</p>
  
  <p>Does anybody have idea about this problem?</p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>I got a new idea about unsetting shipping method in cart page and hiding shipping method in checkout page based on shipping type radio button. As a result, I tried to write the code as below. These codes can work and the label of shipping method can disappear. However, after selecting the one of shipping methods, the other hide shipping methods will fade in. Is there any solution?</p>
  
  <pre><code>//Unset shipping method in cart page
  add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );
  function disable_shipping_calc_on_cart( $show_shipping ) {
      if( is_cart() ) {
          return false;
      }
      return $show_shipping;
  }
  
  //Hide shipping method in checkout page based on the selection of radio button.
  
  add_action( 'woocommerce_before_checkout_shipping_form', 'custom_shipping_radio_button', 10, 1 );
  function custom_shipping_radio_button( $checkout ) {
  
      woocommerce_form_field( 'shipping_type', array(
          'type' =&gt; 'radio',
          'class' =&gt; array( 'form-row-wide' ),
          'label' =&gt; __('收件方式 *'),
          'options' =&gt; array(
              'shipping_1' =&gt; __('全家店到店'),
              'shipping_2' =&gt; __('指定地址'),
              'shipping_3' =&gt; __('自行取貨'),
          ),
      ), $checkout-&gt;get_value( 'shipping_type' ) );
  
    ?&gt;
      &lt;script type="text/javascript"&gt;
      jQuery(function($){
          $("input[name=shipping_type]").on("change",function(){
              if($("#shipping_type_shipping_1").is(":checked")) {
                  $("#add_familimart,#shipping_first_name_field,#shipping_last_name_field,#shipping_city_field,#shipping_company_field,#shipping_method_0_flat_rate9,label[for='shipping_method_0_flat_rate9']").fadeIn();
              } else {
                  $("#add_familimart,#shipping_first_name_field,#shipping_last_name_field,#shipping_city_field,#shipping_company_field,#shipping_method_0_flat_rate9,label[for='shipping_method_0_flat_rate9']").fadeOut();
              }
              if($("#shipping_type_shipping_2").is(":checked")) {
                  $("#shipping_postcode_field,#shipping_address_1_field,#shipping_method_0_flat_rate10,#shipping_method_0_flat_rate11,#shipping_method_0_flat_rate12,label[for='shipping_method_0_flat_rate12'],label[for='shipping_method_0_flat_rate11'],label[for='shipping_method_0_flat_rate10']").fadeIn();
              } else {
                  $("#shipping_postcode_field,#shipping_address_1_field,#shipping_method_0_flat_rate10,#shipping_method_0_flat_rate11,#shipping_method_0_flat_rate12,label[for='shipping_method_0_flat_rate12'],label[for='shipping_method_0_flat_rate11'],label[for='shipping_method_0_flat_rate10']").fadeOut();
              }
            if($("#shipping_type_shipping_3").is(":checked")) { $("#shipping_address_2_field,#shipping_method_0_local_pickup8,label[for='shipping_method_0_local_pickup8']").fadeIn();
              } else {
                  $("#shipping_address_2_field,#shipping_method_0_local_pickup8,label[for='shipping_method_0_local_pickup8']").fadeOut();
              }
          });
      });
  &lt;/script&gt;
      &lt;?php
  }
  </code></pre>