有条件的Woocommerce结帐字段逻辑

I will like to create a custom logical checkout page regarding the fields. I would like to hide fields if the name is 'step' and has a value of '1' or '2'.

That is my code.

function wpb_custom_billing_fields( $fields = array()) {
    if($_POST['post_data']){
        parse_str( $_POST['post_data'], $post_data );
        $_SESSION['post_data'] = $post_data;
    };
    if( $_SESSION['post_data']["step"] == '1' ) {
        unset( $fields['billing']['billing_company'] );
        unset( $fields['billing']['billing_address_1'] );
        unset( $fields['billing']['billing_address_2'] );
        unset( $fields['billing']['billing_city'] );
        unset( $fields['billing']['billing_postcode'] );
        unset( $fields["billing"]["billing_country"] );
        unset( $fields['billing']['billing_state'] );
        unset( $fields['billing']['billing_phone'] );
        unset( $fields['billing']['billing_email'] );
        unset( $fields["billing"]["billing_country"] );
        unset($fields['shipping_address_1']);
        unset($fields['shipping_address_2']);
        unset($fields['shipping_city']);
        unset($fields['shipping_state']);
        unset($fields['shipping_postcode']);

    };
    if( $_SESSION['post_data']["step"] == '2' ) {

        unset( $fields['billing']['billing_company'] );
        unset( $fields['billing']['billing_address_1'] );
        unset( $fields['billing']['billing_address_2'] );
        unset( $fields['billing']['billing_city'] );
        unset( $fields['billing']['billing_postcode'] );
        unset( $fields['billing']['billing_country'] );
        unset( $fields['billing']['billing_state'] );
        unset( $fields['billing']['billing_phone'] );
        unset( $fields['billing']['billing_email'] );
        unset( $fields['last_name'] );
    }
    return $fields;  
}
add_filter('woocommerce_checkout_fields','wpb_custom_billing_fields');

function sv_unrequire_wc_phone_field( $fields ) {
    if( $_SESSION['post_data']["step"] == '1' ) {
        $fields['billing_phone']['required'] = false;
    }
    if( $_SESSION['post_data']["step"] == '2' ) {
        $fields['billing_phone']['required'] = false;
    }
    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field' );

The first function works but second one doesn't work.

Any help is highly appreciated.

Step - 1

Step - 1

Step - 2

Step - 2

        function wpb_custom_billing_fields( $fields = array()) {
        if($_POST['post_data']){
    parse_str( $_POST['post_data'], $post_data );
    // $_SESSION['post_data'] = $post_data;
    // Need create session woocommerce:) usual $_SESSION don't worked:)
    WC()->session->set( 'post_data' , $post_data );
        };
    if( $post_data["step"] == '1' ) {
          unset( $fields['billing']['billing_company'] );
          unset( $fields['billing']['billing_address_1'] );
    // and more....
    };
    if( $post_data["step"] == '2' ) {

              unset( $fields['billing']['billing_company'] );
          unset( $fields['billing']['billing_address_1'] );
          unset( $fields['billing']['billing_address_2'] );
          // and more....
    };


        return $fields;

    }
    add_filter('woocommerce_checkout_fields','wpb_custom_billing_fields');

    function sv_unrequire_wc_phone_field( $fields ) {
 // Get step session
        $step = WC()->session->get( 'post_data' );
            if( $step["step"] == '1' ) {
        $fields['billing_email']['required'] = false;
        return $fields;
    };
        if( $step["step"] == '2' ) {

        return $fields;
    };

    }
    add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field' );

Thanks:)

UPDATE: How it is work

For this code to work, you need to call the update event.

    add_action( 'wp_footer', 'artabr_add_script_update_shipping_method' );
function artabr_add_script_update_shipping_method() {
   if (is_checkout()) {
      ?>
      <script>
        (function( $ ) {
           $(document.body).on('click', '.step ul.reception-tabs li a',  function () {
 $('body').trigger('update_checkout');
});
           })( jQuery );
      </script>
      <?php
   }
}

And create in form hidden input.

    $(".step .reception-tabs li:first").click(function(){
        $('.hidden-step').val(1);
        $('#ship-to-different-address-checkbox').val(0);
        $('.shipping_address').hide();

    });
    $(".step .reception-tabs li:last").click(function(){
        $('.hidden-step').val(2);
        $('#ship-to-different-address-checkbox').val(1);
        $('.shipping_address').show();
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="step">
                <p class="title">Step 1: Select delivery</p>
                <ul class="reception-tabs">
                    <li><a class="active" href="#">Local pickup</a></li>
                    <li><a href="#">Delivery</a></li>
                </ul>
            </div>
<input class="hidden-step" type="hidden" name="step" value="1" />

</div>