如何在 WooCommerce 的结帐字段之间添加标题

How to add a heading in between checkout fields of 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>' ;


}

现在字段是 re-arranged 并且显示自定义标题。但我想要 标题显示在名称和公司字段的正下方。 使用我的代码,该字段被添加到下面。我如何将其重新定位到我想要的位置。

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');

我们可以使用过滤器 'woocommerce_form_field_' . $type... 其中 $type 是输入的类型...在我们的例子中 billing_company 是文本类型...这个过滤器returns 字段的 html,在我们的例子中是 billing 字段,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 .= '<p class="form-row form-row-wide">Custom Heading</p>';
    }
    return $field;
}

重要提示: 如果我们不将其格式化为带有 class form-row 的段落,它将被置于所有账单字段的顶部通过 Woocommerce(我不知道的原因)。这向我们表明这是“Hack”,也许您的目标会以不同的方式实现得更好。

我更喜欢使用 woocommerce_form_field_<field_type> 过滤器来创建新的字段类型,而不是连接到现有的 woocommerce_form_field_<field_type> 过滤器,例如 woocommerce_form_field_text。这允许我们为其他字段类型添加 HTML 输出(因此我们可以使用 HTML 输出作为标题)并且我们可以在修改结帐字段时使用这个新标题 "field type" woocommerce_checkout_fields 过滤器。

// Add field type to WooCommerce form field 
add_filter( 'woocommerce_form_field_heading','sc_woocommerce_form_field_heading', 10, 4 );
function sc_woocommerce_form_field_heading($field, $key, $args, $value) {
    $output = '<h3 class="form-row form-row-wide">'.__( $args['label'], 'woocommerce' ).'</h3>';
    echo $output;
}

// Modify checkout fields
add_filter( 'woocommerce_checkout_fields','custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_heading_name'] = array(
        'type'      => 'heading',
        'label'     => 'Heading here',
    );

使用上述方法原题的解为:

add_filter('woocommerce_checkout_fields', 'ac_checkout_reorder_fields');
function ac_checkout_reorder_fields($fields) {
    $fields['billing']['billing_heading_name'] = array(
        'type'      => 'heading',
        'label'     => 'Heading here',
    );
    $order = array(
        "billing_first_name", 
        "billing_last_name", 
        "billing_heading_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_filter('woocommerce_form_field', 'addHeadingsInBetweenFormFields', 10, 4);

function addHeadingsInBetweenFormFields($field, $key, $args, $value = null) {

  if (is_checkout() & $key === 'billing_first_name') {
    $a = '<p class="form-row form-row-wide">Shipping</p>';
    return $a . $field;
  }
  return $field;

}