2016-03-05 83 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

相關問題