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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?