問題描述
基于此有效答案:
//注冊外部 jQuery/JS 文件函數 cfields_scripts() {//重要的提示://對于子主題,將 get_template_directory_uri() 替換為 get_stylesheet_directory_uri()//外部 cfields.js 文件位于活動子主題或主題的子文件夾js"中.wp_enqueue_script('checkout_script', get_template_directory_uri().'/js/cfields.js', array('jquery'), '1.0', true );}add_action('wp_enqueue_scripts', 'cfields_scripts');add_filter('woocommerce_checkout_fields', 'ba_custom_checkout_billing_fields');功能 ba_custom_checkout_billing_fields( $fields ) {//1. 創建額外的自定義計費字段//狀態"選擇器$fields['billing']['billing_status']['type'] = 'select';$fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');$fields['billing']['billing_status']['required'] = true;$fields['billing']['billing_status']['label'] = __('Statut Juridic', 'theme_domain');$fields['billing']['billing_status']['placeholder'] = __('Alege statutul', 'theme_domain');$fields['billing']['billing_status']['options'] = array('1' =>__( 'Persona Fizica', 'theme_domain' ),'2' =>__( 'Persoana Juridica', 'theme_domain' ));//Nr.registrul comertului"文本字段(這個字段很常見)$fields['billing']['billing_ser_id']['type'] = 'text';$fields['billing']['billing_ser_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_ser_id']['required'] = true;//<== HERE 必須是true",因為它總是被顯示并且需要驗證$fields['billing']['billing_ser_id']['label'] = __('Nr. Reg. Comert', 'theme_domain');$fields['billing']['billing_ser_id']['placeholder'] = __('Introdu numarul', 'theme_domain');//銀行"文本字段$fields['billing']['billing_bt_id']['type'] = 'text';$fields['billing']['billing_bt_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_bt_id']['required'] = false;$fields['billing']['billing_bt_id']['label'] = __('Banca', 'theme_domain');$fields['billing']['billing_bt_id']['placeholder'] = __('Adauga Banca', 'theme_domain');//IBAN"文本字段$fields['billing']['billing_ib_id']['type'] = 'text';$fields['billing']['billing_ib_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_ib_id']['required'] = false;$fields['billing']['billing_ib_id']['label'] = __('IBAN', 'theme_domain');$fields['billing']['billing_ib_id']['placeholder'] = __('Adauga IBAN-ul', 'theme_domain');//CIF"文本字段$fields['billing']['billing_cf_id']['type'] = 'text';$fields['billing']['billing_cf_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_cf_id']['required'] = false;$fields['billing']['billing_cf_id']['label'] = __('Cod Fiscal', 'theme_domain');$fields['billing']['billing_cf_id']['placeholder'] = __('Adauga CIF-ul', 'theme_domain');//2. 對計費字段進行排序$fields_order = 數組('billing_first_name', 'billing_last_name', 'billing_email','billing_phone', 'billing_address_1', 'billing_address_2','billing_postcode', 'billing_city', 'billing_country','billing_status', 'billing_company', 'billing_ser_id','billing_bt_id', 'billing_ib_id', 'billing_cf_id');foreach($fields_order 作為 $field)$ordered_fields[$field] = $fields['billing'][$field];$fields['billing'] = $ordered_fields;//4. 返回 Checkout 自定義計費字段返回 $fields;}//處理結帳(檢查必填字段是否不為空)add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');功能 ba_custom_checkout_field_process() {如果(!$_POST['billing_ser_id'])wc_add_notice( __( '<strong>Nr. Reg. Comert</strong> 是必填字段.', 'theme_domain' ), 'error' );如果(!$_POST['billing_bt_id'])wc_add_notice( __( '<strong>Banca</strong> 是必填字段.', 'theme_domain' ), 'error' );如果(!$_POST['billing_ib_id'])wc_add_notice( __( '<strong>IBAN</strong> 是必填字段.', 'theme_domain' ), 'error' );如果(!$_POST['billing_cf_id'])wc_add_notice( __( '<strong>Cod Fiscal</strong> 是必填字段.', 'theme_domain' ), 'error' );}//使用自定義字段值向訂單添加/更新元數據add_action('woocommerce_checkout_update_order_meta', 'ba_custom_checkout_field_update_order_meta');功能 ba_custom_checkout_field_update_order_meta( $order_id ) {$billing_company = $_POST['billing_company'];$billing_ser_id = $_POST['billing_ser_id'];$billing_bt_id = $_POST['billing_bt_id'];$billing_ib_id = $_POST['billing_ib_id'];$billing_cf_id = $_POST['billing_cf_id'];//對于個人將計費公司重置為"(無值)而不是否"如果 ( !empty($billing_company) && 'no' == $billing_company )update_post_meta( $order_id, '_billing_company', '' );如果 ( !empty($billing_ser_id) )update_post_meta( $order_id, '_billing_ser_id', sanitize_text_field( $billing_ser_id) );//只為公司添加/更新數據如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id )update_post_meta( $order_id, '_billing_bt_id', sanitize_text_field( $billing_bt_id) );//只為公司添加/更新數據如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id )update_post_meta( $order_id, '_billing_ib_id', sanitize_text_field( $billing_ib_id) );//只為公司添加/更新數據如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id )update_post_meta( $order_id, '_billing_cf_id', sanitize_text_field( $billing_cf_id) );}//在訂單編輯頁面上顯示自定義字段標題/值add_action('woocommerce_admin_order_data_after_billing_address', 'ba_custom_checkout_field_display_admin_order_meta', 10, 1);功能 ba_custom_checkout_field_display_admin_order_meta( $order ){$輸出 = '';$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );如果(!空($billing_ser_id)){$output .=''.__( 'Nr. Reg. Comert', 'theme_domain' ) .':' .$billing_ser_id .'</p>';}如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id ){$output .='
'.__( 'Banca', 'theme_domain') .':' .$billing_bt_id .'</p>';}如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id ){$output .='
'.__( 'IBAN', 'theme_domain') .':' .$billing_ib_id .'</p>';}如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id ){$output .='
'.__( '鱈魚財政', 'theme_domain') .':' .$billing_cf_id .'</p>';}回聲$輸出;}
要在客戶訂單視圖、Thankyou 頁面、我的帳戶訂單視圖和電子郵件通知上顯示數據,請在 myfunction.php 文件中添加以下 2 個代碼片段:
//在客戶詳細信息"區域顯示訂單視圖數據add_action('woocommerce_order_details_after_customer_details','ba_add_values_to_order_item_meta', 10, 1 );功能 ba_add_values_to_order_item_meta( $order ) {$輸出 = '';$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );如果 ( !empty($billing_ser_id) )$輸出.= '<tr><th>'.__( "Nr. Reg. Comert:", "woocommerce") .'<td>'.$billing_ser_id .'</td></tr>';如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id )$輸出.= '<tr><th>'.__( "Banca:", "woocommerce") .'<td>'.$billing_bt_id .'</td></tr>';如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id )$輸出.= '<tr><th>'.__( "IBAN:", "woocommerce") .'<td>'.$billing_ib_id .'</td></tr>';如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id )$輸出.= '<tr><th>'.__( "鱈魚財政:", "woocommerce" ) .'<td>'.$billing_cf_id .'</td></tr>';回聲$輸出;}//顯示有關電子郵件通知的數據add_action('woocommerce_email_customer_details','ba_add_values_to_emails_notifications', 15, 4);功能 ba_add_values_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {$output = '';$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );如果 ( !empty($billing_ser_id) )$output .='- '.__( "Nr. Reg. Comert:", "woocommerce") .'</strong><span class="text">'.$billing_ser_id .'</span></li>';如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id )$output .='
- '.__( "Banca:", "woocommerce") .'</strong><span class="text">'.$billing_bt_id .'</span></li>';如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id )$output .='
- '.__( "IBAN:", "woocommerce") .'</strong><span class="text">'.$billing_ib_id .'</span></li>';如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id )$output .='
- '.__( "鱈魚財政:", "woocommerce" ) .'</strong><span class="text">'.$billing_cf_id .'</span></li>';$output .= '</ul>';回聲$輸出;}
Javascript cfields.js 代碼(外部文件):
//這個名為cfields.js"的文件位于您活動子主題或主題的子文件夾js"中jQuery(文檔).ready(函數($){//通用序列號字段if(! $("#billing_ser_id_field").hasClass("validate-required") ){$("#billing_ser_id_field").addClass("validate-required");}//開始時要隱藏的 4 個字段(如果不是Persoana Juridica")if($("#billing_status option:selected").val() == "1"){$('#billing_company_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_company').val("no");});$('#billing_bt_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_bt_id').val("no");});$('#billing_ib_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_ib_id').val("no");});$('#billing_cf_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_cf_id').val("no");});}//選擇器的動作(顯示/隱藏和添加/刪除類)$("#billing_status").change(function(){//對于Persoana Juridica"if($("#billing_status option:selected").val() == "2"){$('#billing_company_field').show(function(){$(this).addClass("validate-required");$('#billing_company').val("");});$('#billing_bt_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_bt_id').val("");});$('#billing_ib_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_ib_id').val("");});$('#billing_cf_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_cf_id').val("");});}//對于Persona Fizica"else if($("#billing_status option:selected").val() == "1"){$('#billing_company_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_company').val("no");});$('#billing_bt_id_field').hide(function(){$(this).children("abbr.required").remove();$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_bt_id').val("no");});$('#billing_ib_id_field').hide(function(){$(this).children("abbr.required").remove();$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_ib_id').val("no");});$('#billing_cf_id_field').hide(function(){$(this).children("abbr.required").remove();$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_cf_id').val("no");});}});});
<塊引用>
我的問題是:
選擇 Persoana Juridica 時,如何隱藏 First name 和 Last name 字段?
謝謝
我做了一些改動:
- 在
ba_custom_checkout_billing_fields
鉤子函數中輕松 - 主要在 javascript 外部文件中...
這是您所有的 PHP 代碼:
//注冊外部 jQuery/JS 文件函數 cfields_scripts() {//重要的提示://對于子主題,將 get_template_directory_uri() 替換為 get_stylesheet_directory_uri()//外部 cfields.js 文件位于活動子主題或主題的子文件夾js"中.wp_enqueue_script('checkout_script', get_template_directory_uri().'/js/cfields.js', array('jquery'), '1.0', true );}add_action('wp_enqueue_scripts', 'cfields_scripts');add_filter('woocommerce_checkout_fields', 'ba_custom_checkout_billing_fields');功能 ba_custom_checkout_billing_fields( $fields ) {//1. 創建額外的自定義計費字段//狀態"選擇器$fields['billing']['billing_status']['type'] = 'select';$fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');$fields['billing']['billing_status']['required'] = true;$fields['billing']['billing_status']['label'] = __('Statut Juridic', 'theme_domain');$fields['billing']['billing_status']['placeholder'] = __('Alege statutul', 'theme_domain');$fields['billing']['billing_status']['options'] = array('1' =>__( 'Persona Fizica', 'theme_domain' ),'2' =>__( 'Persoana Juridica', 'theme_domain' ));//Nr.registrul comertului"文本字段(這個字段很常見)$fields['billing']['billing_ser_id']['type'] = 'text';$fields['billing']['billing_ser_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_ser_id']['required'] = false;$fields['billing']['billing_ser_id']['label'] = __('Nr. Reg. Comert', 'theme_domain');$fields['billing']['billing_ser_id']['placeholder'] = __('Introdu numarul', 'theme_domain');//銀行"文本字段$fields['billing']['billing_bt_id']['type'] = 'text';$fields['billing']['billing_bt_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_bt_id']['required'] = false;$fields['billing']['billing_bt_id']['label'] = __('Banca', 'theme_domain');$fields['billing']['billing_bt_id']['placeholder'] = __('Adauga Banca', 'theme_domain');//IBAN"文本字段$fields['billing']['billing_ib_id']['type'] = 'text';$fields['billing']['billing_ib_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_ib_id']['required'] = false;$fields['billing']['billing_ib_id']['label'] = __('IBAN', 'theme_domain');$fields['billing']['billing_ib_id']['placeholder'] = __('Adauga IBAN-ul', 'theme_domain');//CIF"文本字段$fields['billing']['billing_cf_id']['type'] = 'text';$fields['billing']['billing_cf_id']['class'] = array('form-row-wide', 'status-group2');$fields['billing']['billing_cf_id']['required'] = false;$fields['billing']['billing_cf_id']['label'] = __('Cod Fiscal', 'theme_domain');$fields['billing']['billing_cf_id']['placeholder'] = __('Adauga CIF-ul', 'theme_domain');//2. 更改一些現有的計費字段//名字和姓氏,禁用必需"$fields['billing']['billing_first_name']['required'] = false;$fields['billing']['billing_last_name']['required'] = false;//3. 對計費字段進行排序$fields_order = 數組('billing_first_name', 'billing_last_name', 'billing_email','billing_phone', 'billing_address_1', 'billing_address_2','billing_postcode', 'billing_city', 'billing_country','billing_status', 'billing_company', 'billing_ser_id','billing_bt_id', 'billing_ib_id', 'billing_cf_id');foreach($fields_order 作為 $field)$ordered_fields[$field] = $fields['billing'][$field];$fields['billing'] = $ordered_fields;//4. 返回 Checkout 自定義計費字段返回 $fields;}//處理結帳(檢查必填字段是否不為空)add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');功能 ba_custom_checkout_field_process() {如果(!$_POST['billing_ser_id'])wc_add_notice( __( '<strong>Nr. Reg. Comert</strong> 是必填字段.', 'theme_domain' ), 'error' );如果(!$_POST['billing_bt_id'])wc_add_notice( __( '<strong>Banca</strong> 是必填字段.', 'theme_domain' ), 'error' );如果(!$_POST['billing_ib_id'])wc_add_notice( __( '<strong>IBAN</strong> 是必填字段.', 'theme_domain' ), 'error' );如果(!$_POST['billing_cf_id'])wc_add_notice( __( '<strong>Cod Fiscal</strong> 是必填字段.', 'theme_domain' ), 'error' );}//使用自定義字段值向訂單添加/更新元數據add_action('woocommerce_checkout_update_order_meta', 'ba_custom_checkout_field_update_order_meta');功能 ba_custom_checkout_field_update_order_meta( $order_id ) {$billing_company = $_POST['billing_company'];$billing_ser_id = $_POST['billing_ser_id'];$billing_bt_id = $_POST['billing_bt_id'];$billing_ib_id = $_POST['billing_ib_id'];$billing_cf_id = $_POST['billing_cf_id'];如果 ( !empty($billing_company) && 'no' == $billing_company )update_post_meta( $order_id, '_billing_company', '' );## 這里我們將'no' == $billing_ser_id 添加到條件中如果 ( !empty($billing_ser_id) && 'no' == $billing_ser_id )update_post_meta( $order_id, '_billing_ser_id', sanitize_text_field( $billing_ser_id) );如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id )update_post_meta( $order_id, '_billing_bt_id', sanitize_text_field( $billing_bt_id) );如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id )update_post_meta( $order_id, '_billing_ib_id', sanitize_text_field( $billing_ib_id) );如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id )update_post_meta( $order_id, '_billing_cf_id', sanitize_text_field( $billing_cf_id) );}//在訂單編輯頁面上顯示自定義字段標題/值add_action('woocommerce_admin_order_data_after_billing_address', 'ba_custom_checkout_field_display_admin_order_meta', 10, 1);功能 ba_custom_checkout_field_display_admin_order_meta( $order ){$輸出 = '';$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );如果(!空($billing_ser_id)){$output .=''.__( 'Nr. Reg. Comert', 'theme_domain' ) .':' .$billing_ser_id .'</p>';}如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id ){$output .='
'.__( 'Banca', 'theme_domain') .':' .$billing_bt_id .'</p>';}如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id ){$output .='
'.__( 'IBAN', 'theme_domain') .':' .$billing_ib_id .'</p>';}如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id ){$output .='
'.__( '鱈魚財政', 'theme_domain') .':' .$billing_cf_id .'</p>';}回聲$輸出;}//顯示有關電子郵件通知的數據add_action('woocommerce_email_customer_details','ba_add_values_to_emails_notifications', 15, 4);功能 ba_add_values_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {$output = '
';$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );如果 ( !empty($billing_ser_id) )$output .='- '.__( "Nr. Reg. Comert:", "woocommerce") .'</strong><span class="text">'.$billing_ser_id .'</span></li>';如果 ( !empty($billing_bt_id) && 'no' != $billing_bt_id )$output .='
- '.__( "Banca:", "woocommerce") .'</strong><span class="text">'.$billing_bt_id .'</span></li>';如果 ( !empty($billing_ib_id) && 'no' != $billing_ib_id )$output .='
- '.__( "IBAN:", "woocommerce") .'</strong><span class="text">'.$billing_ib_id .'</span></li>';如果 ( !empty($billing_cf_id) && 'no' != $billing_cf_id )$output .='
- '.__( "鱈魚財政:", "woocommerce" ) .'</strong><span class="text">'.$billing_cf_id .'</span></li>';$output .= '</ul>';回聲$輸出;}
和外部文件cfields.js
Javascript代碼(更新):
//這個名為cfields.js"的文件位于您活動子主題或主題的子文件夾js"中jQuery(文檔).ready(函數($){if($("#billing_status option:selected").val() == "1"){//開始時隱藏字段(不是Persoana Juridica"):$('#billing_company_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_company').val("no");});$('#billing_bt_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_bt_id').val("no");});$('#billing_ib_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_ib_id').val("no");});$('#billing_cf_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_cf_id').val("no");});//開始時顯示字段(不是Persoana Juridica")://序列號字段$('#billing_ser_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_ser_id').val("");});//在開始時設置所需的字段(不是Persoana Juridica")://名字字段$('#billing_first_name_field').children('label').append(' <abbr class="required" title="required">*</abbr>' );$('#billing_first_name_field').addClass("validate-required");//姓氏字段$('#billing_last_name_field').children('label').append(' <abbr class="required" title="required">*</abbr>' );$('#billing_last_name_field').addClass("validate-required");} 別的 {//隱藏序列號字段$('#billing_ser_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_ser_id').val("no");});//隱藏名字字段$('#billing_first_name_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");});//隱藏姓氏字段$('#billing_last_name_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");});}//選擇器的動作(顯示/隱藏和添加/刪除類)$("#billing_status").change(function(){//對于Persoana Juridica"if($("#billing_status option:selected").val() == "2"){//顯示字段:$('#billing_company_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_company').val("");});$('#billing_bt_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_bt_id').val("");});$('#billing_ib_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_ib_id').val("");});$('#billing_cf_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_cf_id').val("");});//隱藏字段://隱藏序列號字段$('#billing_ser_id_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_ser_id').val("no");});//隱藏名字字段$('#billing_first_name_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_first_name').val("no");});//隱藏姓氏字段$('#billing_last_name_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_last_name').val("no");});}//對于Persona Fizica"else if($("#billing_status option:selected").val() == "1"){//隱藏字段:$('#billing_company_field').hide(function(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_company').val("no");});$('#billing_bt_id_field').hide(function(){$(this).children("abbr.required").remove();$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_bt_id').val("no");});$('#billing_ib_id_field').hide(function(){$(this).children("abbr.required").remove();$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_ib_id').val("no");});$('#billing_cf_id_field').hide(function(){$(this).children("abbr.required").remove();$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$('#billing_cf_id').val("no");});//顯示字段://序列號字段$('#billing_ser_id_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_ser_id').val("");});//顯示名字字段$('#billing_first_name_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_first_name').val("");});//顯示姓氏字段$('#billing_last_name_field').show(function(){$(this).children('label').append(' <abbr class="required" title="required">*</abbr>' );$(this).addClass("validate-required");$('#billing_last_name').val("");});}});});
Based on this working answer:
// Registering external jQuery/JS file
function cfields_scripts() {
// IMPORTANT NOTE:
// For a child theme replace get_template_directory_uri() by get_stylesheet_directory_uri()
// The external cfields.js file goes in a subfolder "js" of your active child theme or theme.
wp_enqueue_script( 'checkout_script', get_template_directory_uri().'/js/cfields.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'cfields_scripts' );
add_filter( 'woocommerce_checkout_fields', 'ba_custom_checkout_billing_fields' );
function ba_custom_checkout_billing_fields( $fields ) {
// 1. Creating the additional custom billing fields
// The "status" selector
$fields['billing']['billing_status']['type'] = 'select';
$fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');
$fields['billing']['billing_status']['required'] = true;
$fields['billing']['billing_status']['label'] = __('Statut Juridic', 'theme_domain');
$fields['billing']['billing_status']['placeholder'] = __('Alege statutul', 'theme_domain');
$fields['billing']['billing_status']['options'] = array(
'1' => __( 'Persoana Fizica', 'theme_domain' ),
'2' => __( 'Persoana Juridica', 'theme_domain' )
);
// The "Nr. registrul comertului" text field (this field is common)
$fields['billing']['billing_ser_id']['type'] = 'text';
$fields['billing']['billing_ser_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_ser_id']['required'] = true; // <== HERE has to be "true" as it always be shown and need validation
$fields['billing']['billing_ser_id']['label'] = __('Nr. Reg. Comert', 'theme_domain');
$fields['billing']['billing_ser_id']['placeholder'] = __('Introdu numarul', 'theme_domain');
// The "Banca" text field
$fields['billing']['billing_bt_id']['type'] = 'text';
$fields['billing']['billing_bt_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_bt_id']['required'] = false;
$fields['billing']['billing_bt_id']['label'] = __('Banca', 'theme_domain');
$fields['billing']['billing_bt_id']['placeholder'] = __('Adauga Banca', 'theme_domain');
// The "IBAN" text field
$fields['billing']['billing_ib_id']['type'] = 'text';
$fields['billing']['billing_ib_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_ib_id']['required'] = false;
$fields['billing']['billing_ib_id']['label'] = __('IBAN', 'theme_domain');
$fields['billing']['billing_ib_id']['placeholder'] = __('Adauga IBAN-ul', 'theme_domain');
// The "CIF" text field
$fields['billing']['billing_cf_id']['type'] = 'text';
$fields['billing']['billing_cf_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_cf_id']['required'] = false;
$fields['billing']['billing_cf_id']['label'] = __('Cod Fiscal', 'theme_domain');
$fields['billing']['billing_cf_id']['placeholder'] = __('Adauga CIF-ul', 'theme_domain');
// 2. Ordering the billing fields
$fields_order = array(
'billing_first_name', 'billing_last_name', 'billing_email',
'billing_phone', 'billing_address_1', 'billing_address_2',
'billing_postcode', 'billing_city', 'billing_country',
'billing_status', 'billing_company', 'billing_ser_id',
'billing_bt_id', 'billing_ib_id', 'billing_cf_id'
);
foreach($fields_order as $field)
$ordered_fields[$field] = $fields['billing'][$field];
$fields['billing'] = $ordered_fields;
// 4. Returning Checkout customized billing fields
return $fields;
}
// Process the checkout (Checking if required fields are not empty)
add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');
function ba_custom_checkout_field_process() {
if ( ! $_POST['billing_ser_id'] )
wc_add_notice( __( '<strong>Nr. Reg. Comert</strong> is a required field.', 'theme_domain' ), 'error' );
if ( ! $_POST['billing_bt_id'] )
wc_add_notice( __( '<strong>Banca</strong> is a required field.', 'theme_domain' ), 'error' );
if ( ! $_POST['billing_ib_id'] )
wc_add_notice( __( '<strong>IBAN</strong> is a required field.', 'theme_domain' ), 'error' );
if ( ! $_POST['billing_cf_id'] )
wc_add_notice( __( '<strong>Cod Fiscal</strong> is a required field.', 'theme_domain' ), 'error' );
}
// Adding/Updating meta data to the order with the custom-fields values
add_action( 'woocommerce_checkout_update_order_meta', 'ba_custom_checkout_field_update_order_meta' );
function ba_custom_checkout_field_update_order_meta( $order_id ) {
$billing_company = $_POST['billing_company'];
$billing_ser_id = $_POST['billing_ser_id'];
$billing_bt_id = $_POST['billing_bt_id'];
$billing_ib_id = $_POST['billing_ib_id'];
$billing_cf_id = $_POST['billing_cf_id'];
// For Individual resetting billing company to "" (no value) instead of 'no'
if ( !empty($billing_company) && 'no' == $billing_company )
update_post_meta( $order_id, '_billing_company', '' );
if ( !empty($billing_ser_id) )
update_post_meta( $order_id, '_billing_ser_id', sanitize_text_field( $billing_ser_id ) );
// Adding/updating data only for companies
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
update_post_meta( $order_id, '_billing_bt_id', sanitize_text_field( $billing_bt_id ) );
// Adding/updating data only for companies
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
update_post_meta( $order_id, '_billing_ib_id', sanitize_text_field( $billing_ib_id ) );
// Adding/updating data only for companies
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
update_post_meta( $order_id, '_billing_cf_id', sanitize_text_field( $billing_cf_id ) );
}
// Display custom-field Title/values on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'ba_custom_checkout_field_display_admin_order_meta', 10, 1 );
function ba_custom_checkout_field_display_admin_order_meta( $order ){
$output = '';
$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );
$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );
$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );
if ( !empty($billing_ser_id) ){
$output .= '<p><strong>' . __( 'Nr. Reg. Comert', 'theme_domain' ) . ':</strong> ' . $billing_ser_id . '</p>';
}
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id ){
$output .= '<p><strong>' . __( 'Banca', 'theme_domain' ) . ':</strong> ' . $billing_bt_id . '</p>';
}
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id ){
$output .= '<p><strong>' . __( 'IBAN', 'theme_domain' ) . ':</strong> ' . $billing_ib_id . '</p>';
}
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id ){
$output .= '<p><strong>' . __( 'Cod Fiscal', 'theme_domain' ) . ':</strong> ' . $billing_cf_id . '</p>';
}
echo $output;
}
To display the data on the customer order view, on Thankyou page, My account order view and on email notifications, add this 2 code snippets in myfunction.php file:
// Displaying data on order view in "customer details" zone
add_action('woocommerce_order_details_after_customer_details','ba_add_values_to_order_item_meta', 10, 1 );
function ba_add_values_to_order_item_meta( $order ) {
$output = '';
$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );
$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );
$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );
if ( !empty($billing_ser_id) )
$output .= '
<tr>
<th>' . __( "Nr. Reg. Comert:", "woocommerce" ) . '</th>
<td>' . $billing_ser_id . '</td>
</tr>';
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
$output .= '
<tr>
<th>' . __( "Banca:", "woocommerce" ) . '</th>
<td>' . $billing_bt_id . '</td>
</tr>';
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
$output .= '
<tr>
<th>' . __( "IBAN:", "woocommerce" ) . '</th>
<td>' . $billing_ib_id . '</td>
</tr>';
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
$output .= '
<tr>
<th>' . __( "Cod Fiscal:", "woocommerce" ) . '</th>
<td>' . $billing_cf_id . '</td>
</tr>';
echo $output;
}
// Displaying data on email notifications
add_action('woocommerce_email_customer_details','ba_add_values_to_emails_notifications', 15, 4 );
function ba_add_values_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$output = '<ul>';
$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );
$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );
$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );
if ( !empty($billing_ser_id) )
$output .= '<li><strong>' . __( "Nr. Reg. Comert:", "woocommerce" ) . '</strong> <span class="text">' . $billing_ser_id . '</span></li>';
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
$output .= '<li><strong>' . __( "Banca:", "woocommerce" ) . '</strong> <span class="text">' . $billing_bt_id . '</span></li>';
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
$output .= '<li><strong>' . __( "IBAN:", "woocommerce" ) . '</strong> <span class="text">' . $billing_ib_id . '</span></li>';
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
$output .= '<li><strong>' . __( "Cod Fiscal:", "woocommerce" ) . '</strong> <span class="text">' . $billing_cf_id . '</span></li>';
$output .= '</ul>';
echo $output;
}
Javascript cfields.js code (external file):
// This file named "cfields.js" goes in a subfolder "js" of your active child theme or theme
jQuery(document).ready(function($){
// Common Serial ID field
if(! $("#billing_ser_id_field").hasClass("validate-required") ){
$("#billing_ser_id_field").addClass("validate-required");
}
// The 4 Fields to hide at start (if not "Persoana Juridica")
if($("#billing_status option:selected").val() == "1"){
$('#billing_company_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_company').val("no");
});
$('#billing_bt_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_bt_id').val("no");
});
$('#billing_ib_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_ib_id').val("no");
});
$('#billing_cf_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_cf_id').val("no");
});
}
// Action with the selector (Showing/hiding and adding/removing classes)
$("#billing_status").change(function(){
// For "Persoana Juridica"
if($("#billing_status option:selected").val() == "2")
{
$('#billing_company_field').show(function(){
$(this).addClass("validate-required");
$('#billing_company').val("");
});
$('#billing_bt_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_bt_id').val("");
});
$('#billing_ib_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_ib_id').val("");
});
$('#billing_cf_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_cf_id').val("");
});
}
// For "Persoana Fizica"
else if($("#billing_status option:selected").val() == "1")
{
$('#billing_company_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_company').val("no");
});
$('#billing_bt_id_field').hide(function(){
$(this).children("abbr.required").remove();
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_bt_id').val("no");
});
$('#billing_ib_id_field').hide(function(){
$(this).children("abbr.required").remove();
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_ib_id').val("no");
});
$('#billing_cf_id_field').hide(function(){
$(this).children("abbr.required").remove();
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_cf_id').val("no");
});
}
});
});
My question is:
How can I Hide First name and Last name fields when Persoana Juridica is selected?
Thanks
I have make some changes:
- Lightly in
ba_custom_checkout_billing_fields
hooked function - Mostly in the javascript external file…
So Here is all your PHP code:
// Registering external jQuery/JS file
function cfields_scripts() {
// IMPORTANT NOTE:
// For a child theme replace get_template_directory_uri() by get_stylesheet_directory_uri()
// The external cfields.js file goes in a subfolder "js" of your active child theme or theme.
wp_enqueue_script( 'checkout_script', get_template_directory_uri().'/js/cfields.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'cfields_scripts' );
add_filter( 'woocommerce_checkout_fields', 'ba_custom_checkout_billing_fields' );
function ba_custom_checkout_billing_fields( $fields ) {
// 1. Creating the additional custom billing fields
// The "status" selector
$fields['billing']['billing_status']['type'] = 'select';
$fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');
$fields['billing']['billing_status']['required'] = true;
$fields['billing']['billing_status']['label'] = __('Statut Juridic', 'theme_domain');
$fields['billing']['billing_status']['placeholder'] = __('Alege statutul', 'theme_domain');
$fields['billing']['billing_status']['options'] = array(
'1' => __( 'Persoana Fizica', 'theme_domain' ),
'2' => __( 'Persoana Juridica', 'theme_domain' )
);
// The "Nr. registrul comertului" text field (this field is common)
$fields['billing']['billing_ser_id']['type'] = 'text';
$fields['billing']['billing_ser_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_ser_id']['required'] = false;
$fields['billing']['billing_ser_id']['label'] = __('Nr. Reg. Comert', 'theme_domain');
$fields['billing']['billing_ser_id']['placeholder'] = __('Introdu numarul', 'theme_domain');
// The "Banca" text field
$fields['billing']['billing_bt_id']['type'] = 'text';
$fields['billing']['billing_bt_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_bt_id']['required'] = false;
$fields['billing']['billing_bt_id']['label'] = __('Banca', 'theme_domain');
$fields['billing']['billing_bt_id']['placeholder'] = __('Adauga Banca', 'theme_domain');
// The "IBAN" text field
$fields['billing']['billing_ib_id']['type'] = 'text';
$fields['billing']['billing_ib_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_ib_id']['required'] = false;
$fields['billing']['billing_ib_id']['label'] = __('IBAN', 'theme_domain');
$fields['billing']['billing_ib_id']['placeholder'] = __('Adauga IBAN-ul', 'theme_domain');
// The "CIF" text field
$fields['billing']['billing_cf_id']['type'] = 'text';
$fields['billing']['billing_cf_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_cf_id']['required'] = false;
$fields['billing']['billing_cf_id']['label'] = __('Cod Fiscal', 'theme_domain');
$fields['billing']['billing_cf_id']['placeholder'] = __('Adauga CIF-ul', 'theme_domain');
// 2. Changing some existing billing fields
// First name and Last name, disabling 'required'
$fields['billing']['billing_first_name']['required'] = false;
$fields['billing']['billing_last_name']['required'] = false;
// 3. Ordering the billing fields
$fields_order = array(
'billing_first_name', 'billing_last_name', 'billing_email',
'billing_phone', 'billing_address_1', 'billing_address_2',
'billing_postcode', 'billing_city', 'billing_country',
'billing_status', 'billing_company', 'billing_ser_id',
'billing_bt_id', 'billing_ib_id', 'billing_cf_id'
);
foreach($fields_order as $field)
$ordered_fields[$field] = $fields['billing'][$field];
$fields['billing'] = $ordered_fields;
// 4. Returning Checkout customized billing fields
return $fields;
}
// Process the checkout (Checking if required fields are not empty)
add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');
function ba_custom_checkout_field_process() {
if ( ! $_POST['billing_ser_id'] )
wc_add_notice( __( '<strong>Nr. Reg. Comert</strong> is a required field.', 'theme_domain' ), 'error' );
if ( ! $_POST['billing_bt_id'] )
wc_add_notice( __( '<strong>Banca</strong> is a required field.', 'theme_domain' ), 'error' );
if ( ! $_POST['billing_ib_id'] )
wc_add_notice( __( '<strong>IBAN</strong> is a required field.', 'theme_domain' ), 'error' );
if ( ! $_POST['billing_cf_id'] )
wc_add_notice( __( '<strong>Cod Fiscal</strong> is a required field.', 'theme_domain' ), 'error' );
}
// Adding/Updating meta data to the order with the custom-fields values
add_action( 'woocommerce_checkout_update_order_meta', 'ba_custom_checkout_field_update_order_meta' );
function ba_custom_checkout_field_update_order_meta( $order_id ) {
$billing_company = $_POST['billing_company'];
$billing_ser_id = $_POST['billing_ser_id'];
$billing_bt_id = $_POST['billing_bt_id'];
$billing_ib_id = $_POST['billing_ib_id'];
$billing_cf_id = $_POST['billing_cf_id'];
if ( !empty($billing_company) && 'no' == $billing_company )
update_post_meta( $order_id, '_billing_company', '' );
## Here we add 'no' == $billing_ser_id to the condition
if ( !empty($billing_ser_id) && 'no' == $billing_ser_id )
update_post_meta( $order_id, '_billing_ser_id', sanitize_text_field( $billing_ser_id ) );
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
update_post_meta( $order_id, '_billing_bt_id', sanitize_text_field( $billing_bt_id ) );
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
update_post_meta( $order_id, '_billing_ib_id', sanitize_text_field( $billing_ib_id ) );
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
update_post_meta( $order_id, '_billing_cf_id', sanitize_text_field( $billing_cf_id ) );
}
// Display custom-field Title/values on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'ba_custom_checkout_field_display_admin_order_meta', 10, 1 );
function ba_custom_checkout_field_display_admin_order_meta( $order ){
$output = '';
$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );
$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );
$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );
if ( !empty($billing_ser_id) ){
$output .= '<p><strong>' . __( 'Nr. Reg. Comert', 'theme_domain' ) . ':</strong> ' . $billing_ser_id . '</p>';
}
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id ){
$output .= '<p><strong>' . __( 'Banca', 'theme_domain' ) . ':</strong> ' . $billing_bt_id . '</p>';
}
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id ){
$output .= '<p><strong>' . __( 'IBAN', 'theme_domain' ) . ':</strong> ' . $billing_ib_id . '</p>';
}
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id ){
$output .= '<p><strong>' . __( 'Cod Fiscal', 'theme_domain' ) . ':</strong> ' . $billing_cf_id . '</p>';
}
echo $output;
}
// Displaying data on email notifications
add_action('woocommerce_email_customer_details','ba_add_values_to_emails_notifications', 15, 4 );
function ba_add_values_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$output = '<ul>';
$billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
$billing_bt_id = get_post_meta( $order->id, '_billing_bt_id', true );
$billing_ib_id = get_post_meta( $order->id, '_billing_ib_id', true );
$billing_cf_id = get_post_meta( $order->id, '_billing_cf_id', true );
if ( !empty($billing_ser_id) )
$output .= '<li><strong>' . __( "Nr. Reg. Comert:", "woocommerce" ) . '</strong> <span class="text">' . $billing_ser_id . '</span></li>';
if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
$output .= '<li><strong>' . __( "Banca:", "woocommerce" ) . '</strong> <span class="text">' . $billing_bt_id . '</span></li>';
if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
$output .= '<li><strong>' . __( "IBAN:", "woocommerce" ) . '</strong> <span class="text">' . $billing_ib_id . '</span></li>';
if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
$output .= '<li><strong>' . __( "Cod Fiscal:", "woocommerce" ) . '</strong> <span class="text">' . $billing_cf_id . '</span></li>';
$output .= '</ul>';
echo $output;
}
And the external file cfields.js
Javascript code (Updated):
// This file named "cfields.js" goes in a subfolder "js" of your active child theme or theme
jQuery(document).ready(function($){
if($("#billing_status option:selected").val() == "1"){
// HIDING FIELDS AT START (NOT "Persoana Juridica"):
$('#billing_company_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_company').val("no");
});
$('#billing_bt_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_bt_id').val("no");
});
$('#billing_ib_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_ib_id').val("no");
});
$('#billing_cf_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_cf_id').val("no");
});
// SHOWING FIELDS AT START (NOT "Persoana Juridica"):
// Serial ID field
$('#billing_ser_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_ser_id').val("");
});
// SETTING REQUIRED FIELDS AT START (NOT "Persoana Juridica"):
// First name field
$('#billing_first_name_field').children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$('#billing_first_name_field').addClass("validate-required");
// Last name field
$('#billing_last_name_field').children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$('#billing_last_name_field').addClass("validate-required");
} else {
// Hide Serial ID field
$('#billing_ser_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_ser_id').val("no");
});
// Hide First Name field
$('#billing_first_name_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
});
// Hide Last Name field
$('#billing_last_name_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
});
}
// Action with the selector (Showing/hiding and adding/removing classes)
$("#billing_status").change(function(){
// For "Persoana Juridica"
if($("#billing_status option:selected").val() == "2")
{
// SHOWING FIELDS:
$('#billing_company_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_company').val("");
});
$('#billing_bt_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_bt_id').val("");
});
$('#billing_ib_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_ib_id').val("");
});
$('#billing_cf_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_cf_id').val("");
});
// HIDING FIELDS:
// Hide Serial ID field
$('#billing_ser_id_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_ser_id').val("no");
});
// Hide First Name field
$('#billing_first_name_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_first_name').val("no");
});
// Hide Last Name field
$('#billing_last_name_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_last_name').val("no");
});
}
// For "Persoana Fizica"
else if($("#billing_status option:selected").val() == "1")
{
// HIDING FIELDS:
$('#billing_company_field').hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_company').val("no");
});
$('#billing_bt_id_field').hide(function(){
$(this).children("abbr.required").remove();
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_bt_id').val("no");
});
$('#billing_ib_id_field').hide(function(){
$(this).children("abbr.required").remove();
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_ib_id').val("no");
});
$('#billing_cf_id_field').hide(function(){
$(this).children("abbr.required").remove();
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$('#billing_cf_id').val("no");
});
// SHOWING FIELDS:
// Serial ID field
$('#billing_ser_id_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_ser_id').val("");
});
// Showing First Name field
$('#billing_first_name_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_first_name').val("");
});
// Showing Last name field
$('#billing_last_name_field').show(function(){
$(this).children('label').append( ' <abbr class="required" title="required">*</abbr>' );
$(this).addClass("validate-required");
$('#billing_last_name').val("");
});
}
});
});
這篇關于結帳字段:隱藏和顯示現有字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!