2017-02-20 130 views
1

我得到此代碼以將自定義字段添加到WooCommerce Billing表單。將自定義字段添加到WooCommerce結算表單中?

該字段被示出,但問題是該字段具有不label也不placeholder也不class name

我在這裏錯過了什麼? 我將這段代碼添加到我的子主題的functions.php中。

/******************************* 
    CUSTOM BILLING FIELD 
******************************** */ 
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields'); 

function custom_woocommerce_billing_fields($fields) 
{ 

    $fields['billing']['billing_options'] = array(
     'label' => __('NIF', 'woocommerce'), // Add custom field label 
     'placeholder' => _x('Your NIF here....', 'placeholder', 'woocommerce'), // Add custom field placeholder 
     'required' => false, // if field is required or not 
     'clear' => false, // add clear or not 
     'type' => 'text', // add field type 
     'class' => array('my-css') // add class name 
    ); 

    return $fields; 
} 
+0

您可以將custombilling字段的代碼添加到'class-wc-countries.php'文件中的'get_default_address_fields()'函數 –

回答

2

如果您正在使用woocommerce_billing_fields那麼你並不需要 指定它會自動獲得分配給計費 字段的字段。但是,如果您使用的是woocommerce_checkout_fields,那麼只有 您需要指定您需要shippingbilling的字段。

woocommerce_billing_fields對於

add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields'); 

function custom_woocommerce_billing_fields($fields) 
{ 

    $fields['billing_options'] = array(
     'label' => __('NIF', 'woocommerce'), // Add custom field label 
     'placeholder' => _x('Your NIF here....', 'placeholder', 'woocommerce'), // Add custom field placeholder 
     'required' => false, // if field is required or not 
     'clear' => false, // add clear or not 
     'type' => 'text', // add field type 
     'class' => array('my-css') // add class name 
    ); 

    return $fields; 
} 


對於woocommerce_checkout_fields

add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields'); 

function custom_woocommerce_billing_fields($fields) 
{ 
    $fields['billing']['billing_options'] = array(
     'label' => __('NIF', 'woocommerce'), // Add custom field label 
     'placeholder' => _x('Your NIF here....', 'placeholder', 'woocommerce'), // Add custom field placeholder 
     'required' => false, // if field is required or not 
     'clear' => false, // add clear or not 
     'type' => 'text', // add field type 
     'class' => array('my-css') // add class name 
    ); 

    return $fields; 
} 

參考:

跳這有助於!

+0

我不工作。帳單表單中的所有字段都將被刪除。而新的字段不會被添加。沒有顯示錯誤。 – JPashs

+0

@JPashs:我沒有測試我的代碼,只是從我的大腦中進行測試,但是在您的鉤子和數組參數中存在邏輯錯誤,您正在使用'woocommerce_billing_fields'並再次添加'['billing']',因此您沒有獲得所需的輸出。只需用這個'woocommerce_checkout_fields'替換你的鉤子,並保持你提到的功能是問題,那麼它也會工作。 –

+0

我試過這個:http://pastebin.com/raw/JrnHfuVT但還沒有工作。你能告訴我缺少什麼,並在你的答案中改變代碼。這樣我就可以接受你的答案。謝謝。 – JPashs