php - woocommerce 中有条件地未设置结帐字段

标签 php jquery wordpress woocommerce checkout

如何有条件地从服务器端取消设置其他两个字段并从中删除所需的验证?

表单如下所示: enter image description here

这是代码:

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' );

最佳答案

 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;
        }

关于php - woocommerce 中有条件地未设置结帐字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099213/

相关文章:

php - 如何在magento中的购物车中添加自定义数据

javascript - jquery 函数的较短版本

javascript - 我需要为我的 Chrome 扩展程序提供什么权限(如果有)才能让它进行远程 AJAX 调用?

php - 点击加载更多 WordPress 帖子

php - 从 woocommerce 中的电子邮件模板中删除订单信息部分

wordpress - 在woo-commerce中更改商店URL

javascript - 单击切换到不同的按钮

PHP.ini 文件更改未生效(内存限制)

javascript对象文字模式构造函数错误

php - 如何从字符串中取出所有数字