dongqiabei7682 2018-07-18 19:52
浏览 63
已采纳

有条件的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

  • 写回答

1条回答 默认 最新

  • doulang6695 2018-07-18 21:44
    关注
            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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试