dongrong8972 2016-05-08 11:17
浏览 22
已采纳

在woocommerce中有条不紊地设置结帐领域

How can I conditionally unset the other two fields from server side and remove the required validation from it?

Here is how the form looks like: enter image description here

Here is the code:

function bs_filter_checkout_fields($fields){
    $fields['billing'] = array(

        'add_type' => array(
            'type' => 'radio',
            'label' => __( 'Address Type' ),
            'options' => array( 'house' => __( 'House' ), 'building' => __( 'Building' ), 'office' => __( 'Office' ) ),
            'required' => true
                ),
        'add_house_name' => array(
            'type' => 'text',
            'required'      => true,
            'placeholder' => __( 'House Name/Number' ),
            'label' => __( 'House Name/Number' )
        ),
        'add_building_name' => array(
            'type' => 'text',
            'required'  => true,
            'placeholder' => __( 'Building Name/Number' ),
            'label' => __( 'Building Name/Number' )
        ),
        'add_office_name' => array(
            'type' => 'text',
            'required'  => true,
            'placeholder' => __( 'Office Name/Number' ),
            'label' => __( 'Office Name/Number' )
        ),
    );
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'bs_filter_checkout_fields' );
function bs_conditional_scripts() {
    wc_enqueue_js( "
        $( '#add_type_house' ).change( function () {

            if ( $( this ).is( ':checked' ) ) {
                $('#add_building_name_field').hide();
                $('#add_office_name_field').hide();
                $( '#add_house_name_field' ).show();
            }

        } ).change();

    $( '#add_type_building' ).change( function () {

        if ( $( this ).is( ':checked' ) ) {
            $( '#add_house_name_field' ).hide();
            $('#add_office_name_field').hide();
            $('#add_building_name_field').show();
        }

    } ).change();


    $( '#add_type_office' ).change( function () {

        if ( $( this ).is( ':checked' ) ) {
            $( '#add_house_name_field' ).hide();
            $('#add_building_name_field').hide();
            $('#add_office_name_field').show();
        }

    } ).change();
    " );
}

add_action( 'wp_enqueue_scripts', 'bs_conditional_scripts' );
  • 写回答

1条回答 默认 最新

  • dtm37893 2016-05-08 12:08
    关注
     function bs_filter_checkout_fields($fields){ 
                         $fields['billing'] = array(
                              'add_type' => array(
                                    'type' => 'radio',
                                    'label' => __( 'Address Type' ),
                                    'options' => array( 'house' => __( 'House' ), 'building' => __( 'Building' ), 'office' => __( 'Office' ) ),
                                    'required' => true
                                    ),
                                'add_house_name' => array(
                                    'type' => 'text',
                                    'required'      => true,
                                    'placeholder' => __( 'House Name/Number' ),
                                    'label' => __( 'House Name/Number' )
                                    ),
                                'add_building_name' => array(
                                    'type' => 'text',
                                    'required'  => true,
                                    'placeholder' => __( 'Building Name/Number' ),
                                    'label' => __( 'Building Name/Number' )
                                    ),
                                'add_office_name' => array(
                                    'type' => 'text',
                                    'required'  => true,
                                    'placeholder' => __( 'Office Name/Number' ),
                                    'label' => __( 'Office Name/Number' )
                                    ),
                                );
            if( true ){ // pass conditional statement here
              unset($fields['billing']['add_house_name']);  //  remove field
              $fields['billing']['add_building_name']['required']   = false; // remove required validation
            }                   
            return $fields;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch