2016-08-24 31 views
3

我用這個代碼來創建自定義結賬字段和重新排序字段:自定義下拉選擇顯示或隱藏其他結帳的自定義字段

add_filter('woocommerce_checkout_fields', 'custom_checkout_billing_fields'); 
function 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'] = __('User status', 'my_theme_slug'); 
    $fields['billing']['billing_status']['placeholder'] = __('Chose an option', 'my_theme_slug'); 
    $fields['billing']['billing_status']['options'] = array(
     '' => 'Chose an option', 
     '1' => 'Legal entity', 
     '2' => 'Individual' 
    ); 

    // The "Serial ID" text field 
    $fields['billing']['billing_number_id']['type'] = 'text'; 
    $fields['billing']['billing_number_id']['class'] = array('form-row-wide', 'status-group1'); 
    $fields['billing']['billing_number_id']['required'] = true; 
    $fields['billing']['billing_number_id']['label'] = __('Serial ID', 'my_theme_slug'); 
    $fields['billing']['billing_number_id']['placeholder'] = __('Enter your Serial ID', 'my_theme_slug'); 

    // Customizing 'billing_company' field ['required'] 
    $fields['billing']['billing_company']['required'] = false; 

    // The "Serial ID" text field 
    $fields['billing']['billing_serial']['type'] = 'text'; 
    $fields['billing']['billing_serial']['class'] = array('form-row-wide', 'status-group1'); 
    $fields['billing']['billing_serial']['required'] = false; 
    $fields['billing']['billing_serial']['label'] = __('Serial ID', 'my_theme_slug'); 
    $fields['billing']['billing_serial']['placeholder'] = __('Enter your Serial ID', 'my_theme_slug'); 


// 2. Customizing 'billing_email' and 'billing_phone' fields ['class'] 

    $fields['billing']['billing_email']['class'] = array('form-row-first', 'status-group1'); 
    $fields['billing']['billing_phone']['class'] = array('form-row-last', 'status-group1'); 


// 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_number_id', 'billing_serial' 
    ); 
    foreach($fields_order as $field) $ordered_fields[$field] = $fields['billing'][$field]; 

    $fields['billing'] = $ordered_fields; 


// Returning Checkout customized billing fields 

    return $fields; 

} 

有條件的機制:

  1. 默認情況下,billing_status下拉選擇器將在Individual期權價值和將顯示只有billing_serial自定義字段。
  2. billing_status下拉選擇將切換到"Company" **的選項值,2個更多的字段將出現:
    • billing_company現有場(前billing_serial
    • billing_number_id自定義字段(在結束)

我該如何做到這一點?

由於


參考:WooCommerce - Checkout conditional fields for different persons custom status

+0

這很簡單,用戶將完成唯一的名字,姓氏,在此之後,如果他是法人或個人,他將選擇他的身份。 如果他將從下拉選擇器菜單中選擇個人狀態,則需要顯示的字段將爲電話號碼,地址,電子郵件地址和序列號。 如果他是法人,我需要以下字段: - 電話 - 電子郵件 - 地址 - 和3個其他領域 –

+0

好吧,我想這種結構的原因 - 爲公司 序列ID號如果我想根據人的類型將這些數據納入發票。現在有道理? –

+0

將數據帶到謝謝和發票將是未來的其他問題,我認爲:) – LoicTheAztec

回答

2

對於WooCommerce 3+ (更新)

由於WooCommerce 3.0結帳字段已經改變一點點,從而不可能將字段重新排序爲befo回覆。

有一個新的「優先」說法該處理場訂購,用於結帳領域和我的帳戶領域也是如此。

下面我只更新部分相關訂貨領域:

## 3. Ordering the billing fields 

// Set the order of the fields 
$billing_fields_new_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_number_id', 'billing_ser_id' 
); 

$count = 0; 
$priority = 10; 

// Updating the 'priority' argument 
foreach($billing_fields_new_order as $field_name){ 
    $count++; 
    $fields['billing'][$field_name]['priority'] = $count * $priority; 
} 

// END: returning the customized checkout fields 
return $fields; 

參考:Reordering checkout fields in WooCommerce 3


原來的答覆:

這裏是一個解決方案用jQu使用條件機制工作正如你所期望的... ...的PHP代碼是完成了一些必要的功能...

PHP代碼(進入功能。PHP):

// 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', 'custom_checkout_billing_fields'); 
function 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'] = __('User status', 'my_theme_slug'); 
    $fields['billing']['billing_status']['placeholder'] = __('Chose an option', 'my_theme_slug'); 
    $fields['billing']['billing_status']['options'] = array(
     '1' => __('Individual', ''), 
     '2' => __('Company', '') 
    ); 

    // Customizing 'billing_company' field ['required'] 
    $fields['billing']['billing_company']['required'] = false; 
    $fields['billing']['billing_company']['class'] = array('form-row-wide', 'status-group2'); 

    // The "Number ID" text field 
    $fields['billing']['billing_number_id']['type'] = 'text'; 
    $fields['billing']['billing_number_id']['class'] = array('form-row-wide'); 
    $fields['billing']['billing_number_id']['required'] = true; 
    $fields['billing']['billing_number_id']['label'] = __('Number ID', 'my_theme_slug'); 
    $fields['billing']['billing_number_id']['placeholder'] = __('Enter your Number ID', 'my_theme_slug'); 

    // The "Serial identification" text field 
    $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'] = __('Serial identification', 'my_theme_slug'); 
    $fields['billing']['billing_ser_id']['placeholder'] = __('Enter your Serial identification', 'my_theme_slug'); 


// 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_number_id', 'billing_ser_id' 
    ); 
    foreach($fields_order as $field) $ordered_fields[$field] = $fields['billing'][$field]; 

    $fields['billing'] = $ordered_fields; 


// Returning Checkout customized billing fields 

    return $fields; 

} 


// Process the checkout 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 
function custom_checkout_field_process() { 
    // Check if set, if its not set add an error. 
    if (! $_POST['billing_number_id']) 
     wc_add_notice(__('Please enter your Number ID.', 'my_theme_slug'), 'error'); 
    if (! $_POST['billing_ser_id']) 
     wc_add_notice(__('Please enter your Serial id.' , 'my_theme_slug'), 'error'); 
} 

// Update the order meta with field value 
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta'); 
function custom_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST['billing_number_id'])) 
     update_post_meta($order_id, 'billing_number_id', sanitize_text_field($_POST['billing_number_id'])); 
    if (! empty($_POST['billing_ser_id'])) 
     update_post_meta($order_id, 'billing_ser_id', sanitize_text_field($_POST['billing_ser_id'])); 
} 


// Display field value on the order edit page 
add_action('woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1); 
function custom_checkout_field_display_admin_order_meta($order){ 
    echo '<p><strong>'.__('My number ID').':</strong> ' . get_post_meta($order->id, 'billing_number_id', true) . '</p>'; 
    echo '<p><strong>'.__('My serial identification').':</strong> ' . get_post_meta($order->id, 'billing_ser_id', true) . '</p>'; 
} 

的Javascript cfields.js代碼(外部文件):

// This file named "cfields.js" goes in a subfolder "js" of your active child theme or theme 

jQuery(document).ready(function($){ 

    $('#billing_company_field').hide(function(){ 
     $(this).removeClass("validate-required"); 
    }); 
    $('#billing_ser_id_field').hide(function(){ 
     $(this).removeClass("validate-required"); 
    }); 
    $("#billing_number_id_field").addClass("validate-required"); 

    $("#billing_status").change(function(){ 
     if($("#billing_status option:selected").val() == "2"){ 
      $('#billing_company_field').show(function(){ 
       $(this).addClass("validate-required"); 
      }); 
      $('#billing_ser_id_field').show(function(){ 
       $(this).addClass("validate-required"); 
      }); 
     } else if($("#billing_status option:selected").val() == "1"){ 
      $('#billing_company_field').hide(function(){ 
       $(this).removeClass("validate-required"); 
      }); 
      $('#billing_ser_id_field').hide(function(){ 
       $(this).removeClass("validate-required"); 
      }); 
     } 

    }); 

}); 

所有這些代碼已經被測試和工程

+0

您好Loic,我有一個小問題,我想爲法律實體添加3個字段:銀行名稱,銀行賬戶和註冊號碼..我不能這樣做,你能幫助我,在此先感謝.. @loictheaztec –

相關問題