在选中另一个自定义字段复选框后,动态显示并创建所需的自定义Woocommerce结帐字段

问题描述

我正在搜索一段时间以实现我对标题的描述。

这是我现在在子主题的functions.PHP文件中使用的代码

//--------------------add custom fields to billing section--------------------------------------------
add_filter( 'woocommerce_checkout_fields','display_checkBox_and_new_checkout_field',20 );
  
function display_checkBox_and_new_checkout_field( $fields ) {
  
$fields['billing']['checkBox_trigger'] = array(
    'type'      => 'checkBox','label'     => __('Επιθυμώ Τιμολόγιο','woocommerce'),'class'     => array('form-row-wide'),'clear'     => true
);   
    
$fields['billing']['afm_field'] = array(
    'label'     => __('ΑΦΜ Εταιρείας','placeholder'   => _x('καταχωρείστε το ΑΦΜ σας...','placeholder',//'required' => true,'clear'     => true
);
  
return $fields;
  
}


//toggle feature for the custom field that I need to make required after checkBox is clicked

add_action( 'woocommerce_after_checkout_form','conditionally_hide_show_new_field',9999 );
  
function conditionally_hide_show_new_field() {
    
  wc_enqueue_js( "
      jQuery('input#checkBox_trigger').change(function(){
           
         if (! this.checked) {
            // HIDE IF NOT CHECKED
            jQuery('#afm_field_field').hide();
            jQuery('#afm_field_field').removeClass('validate-required');
         } else {
            // SHOW IF CHECKED
            jQuery('#afm_field_field').show();
            jQuery('#afm_field_field').addClass('validate-required');
         }
           
      }).change();
  ");
       
}

我也已经尝试了以下方法,但似乎不起作用:

add_filter( 'woocommerce_checkout_process','if_checked_make_afm_required',20 );
  
function if_checked_make_afm_required() {
    
    $step = WC()->session->get( 'post_data' );
    
    if ( isset($step['checkBox_trigger']) ) {
        $fields['billing']['afm_field']['required'] = true;
    } else {
        $fields['billing']['afm_field']['required'] = false;
    }
  
}

如果有人可以在这里提供帮助,我将非常感谢。预先感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)