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

时间:2016-03-05 07:19:20

标签: wordpress woocommerce checkout

我正在自定义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>' ;


}

现在重新排列字段并显示自定义标题。但是我希望显示在名称&amp;下面的标题公司领域。使用我的代码,该字段将在下面添加。我如何在我想要的地方重新定位。

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

3 个答案:

答案 0 :(得分:10)

我们可以使用过滤器'woocommerce_form_field_' . $type ...其中$type是输入的类型...在我们的例子中billing_company是text类型...此过滤器返回该字段的html,在我们的案例结算字段中,billing_company ..过滤器有4个参数被传递,这些参数是$field$key$args,{{1} } ...我们只需要其中两个...

$value

答案 1 :(得分:1)

我更愿意使用woocommerce_form_field_<field_type>过滤器来创建新的字段类型,而不是挂钩到现有的woocommerce_form_field_text过滤器,例如woocommerce_form_field_<field_type>。这允许我们为其他字段类型添加HTML输出(因此我们可以使用HTML输出作为标题),我们可以使用这个新标题&#34;字段类型&#34;使用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;
}

答案 2 :(得分:0)

add_filter('woocommerce_form_field', 'mmodify_wc_checkout_pge', 10, 4);

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


    switch ($key) {
        case 'billing_email':


            $a = '<p class="form-row form-row-wide" id="main_heading" data-priority="110"><label for="" class="main-address" style="font-weight:bold; font-size:22px;">Main address&nbsp;</label></p>';

            return $a . $field;
            break;
        default:
            return $field;
            break;
    }
}