2017-02-20 128 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