如何在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>' ;

}

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

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是文本类型...此过滤器返回字段的 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 .= '<p class="form-row form-row-wide">Custom Heading</p>';
    }
    return $field;
}

重要提示:如果我们不将其格式化为带有类form-row的段落,则Woocommerce将其置于所有计费字段的顶部(我不知道原因)。这向我们表明,这是"黑客",也许你的目标以不同的方式实现得更好。

与其挂钩到现有的woocommerce_form_field_<field_type>过滤器(如 woocommerce_form_field_text),我更喜欢使用 woocommerce_form_field_<field_type> 过滤器来创建新的字段类型。这允许我们为其他字段类型添加 HTML 输出(因此我们可以将 HTML 输出用于标题),并且在使用 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;
}