2016-03-05 82 views
3

我正在自定义WooCommerce结帐页面字段。我想在字段之间添加一个标题(文本)。我已经重新排序这样如何在Woocommerce的结帐字段之间添加标题

add_filter('woocommerce_checkout_fields', 'ac_checkout_reorder_fields'); 

function ac_checkout_reorder_fields($fields) { 

    $order = array(
     "billing_first_name", 
     "billing_last_name", 
     "billing_company", 
     "billing_email", 
     "billing_phone", 
     "billing_address_1", 
     "billing_address_2", 
     "billing_postcode", 
     "billing_country" 


    ); 
    foreach($order as $field) 
    { 
     $ordered_fields[$field] = $fields["billing"][$field]; 
    } 

    $fields["billing"] = $ordered_fields; 
    return $fields; 

} 

领域然后我说这样

add_action('woocommerce_after_checkout_billing_form', 'add_custom_heading'); 

function add_custom_heading($checkout) { 

    echo '<div id="add_custom_heading"><h2>' . __('Custom Heading Here') . '</h2></div>' ; 


} 

现在的字段重新安排和自定义标题是显示一个新的标题。但我想标题显示在名称&公司字段下面。随着我的代码,该字段被添加到下面。我如何在我想要的地方重新定位。

PS:我也尝试添加自定义整个字段部分与此挂钩woocommerce_checkout_fields添加占位符和删除标签。这里是代码,但这也不能帮助我解决问题。

function add_wc_custom_fields($fields) { 
global $woocommerce; 
    $countries_obj = new WC_Countries(); 
    $countries = $countries_obj->__get('countries'); 

    $fields['billing']['billing_first_name'] = array(
      'label' => '', 
      'placeholder' => _x('First Name*', 'placeholder', 'woocommerce'), 
      'required' => true, 
      'class' => array('form-row-first'), 
     ); 
     $fields['billing']['billing_last_name'] = array(
      'label' => '', 
      'placeholder' => _x('last Name*', 'placeholder', 'woocommerce'), 
      'required' => true, 
      'class' => array('form-row-last'), 
     ); 
     $fields['billing']['billing_company'] = array(
      'label' => '', 
      'placeholder' => _x('Company Name', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('checkout-billing-company') 
     ); 
     $fields['billing']['billing_address_1'] = array(
      'label' => '', 
      'placeholder' => _x('Address(Line 1)*', 'placeholder', 'woocommerce'), 
      'required' => true, 
      'class' => array('checkout-billing-addressL1') 
     ); 
     $fields['billing']['billing_address_2'] = array(
      'label' => '', 
      'placeholder' => _x('Address(Line 2)*', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('checkout-billing-addressL2') 
     ); 
     $fields['billing']['billing_email'] = array(
      'label' => '', 
      'placeholder' => _x('Email Address*', 'placeholder', 'woocommerce'), 
      'required' => true, 
      'class' => array('form-row-first') 
     ); 
     $fields['billing']['billing_phone'] = array(
      'label' => '', 
      'placeholder' => _x('Phone Number', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('form-row-last') 
     ); 
     $fields['billing']['billing_city'] = array(
      'label' => '', 
      'placeholder' => _x('Town/City', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('form-row-first') 
     ); 

$fields['billing']['billing_state'] = array(
      'label' => '', 
      'placeholder' => _x('State/County', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('form-row-first') 
     ); 
    $fields['billing']['billing_postcode'] = array(
      'label' => '', 
      'placeholder' => _x('Zip/Postcode', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('form-row-first') 
     ); 
     $fields['billing']['billing_country'] = array(
      'label' => '', 
      'type' => 'select', 
      'placeholder' => _x('Country', 'placeholder', 'woocommerce'), 
      'required' => false, 
      'class' => array('form-row-last'), 
       'options' => $countries 
     ); 
     return $fields; 
    } 

add_filter('woocommerce_checkout_fields', 'add_wc_custom_fields'); 
+0

这是很难说的时候都可以看到是php代码... – Reigel

+0

我的错误。这是截图。 http://i.imgur.com/rsTvwFh.png – Ayanize

回答

6

我们可以使用过滤'woocommerce_form_field_' . $type ...其中$type是输入的类型......在我们的情况billing_company是文本类型。这个过滤器返回字段的HTML,在我们的案例计费领域,billing_company ..过滤器已经被传递4个参数,这些$field$key$args$value ...我们只需要其中的两个...

add_action('woocommerce_form_field_text','reigel_custom_heading', 10, 2); 
function reigel_custom_heading($field, $key){ 
    // will only execute if the field is billing_company and we are on the checkout page... 
    if (is_checkout() && ($key == 'billing_company')) { 
     $field .= '<div id="add_custom_heading"><h2>' . __('Custom Heading Here') . '</h2></div>'; 
    } 
    return $field; 
} 
+0

这真棒。我真的正在寻找那个hook woocommerce_form_field。非常感谢你。这解决了我的问题,答案很好。谢啦。 :) – Ayanize

+0

你能否建议如何做同样的事情,但要勾住帐户用户名上方和帐单邮件字段下面的标题?我尝试过'id =“billing_email”'但它没有效果。 – inspirednz

+0

@inspirednz,我不明白。也许你应该创建另一个问题并添加屏幕截图。还要描述并包含您在该问题中迄今为止所尝试的内容。 – Reigel

相关问题