如何在Woocommerce中的woocommerce-account-fields上方添加标题 [英] How to add a heading above woocommerce-account-fields in Woocommerce

查看:83
本文介绍了如何在Woocommerce中的woocommerce-account-fields上方添加标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与在这里询问.我怀疑,答案也将非常类似于此处.

My question is very similar to the one asked here. The answer, I suspect, will also be very similar to the one here.

结帐页面显示典型的帐单"字段.在这些类下面是div类woocommerce-account-fields,其中是div类create-account.嵌套的表单具有两个字段account_usernameaccount_password.

The checkout page displays the typical Billing fields. Below those is the div class woocommerce-account-fields, in which is the div class create-account. The form nested in that has two fields, account_username and account_password.

我想插入此标题,并在最后一个计费字段(billing_email)下方和account_username字段上方显示标题.

I want to hook into this and display a heading below the last billing field (billing_email) and above the account_username field.

阅读上面提到的问题和答案,我认为我可以做这样的事情:

Reading the above mentioned question and answer, I figured I'd be able to do something like this:

add_action( 'woocommerce_form_field_text','XYZ_checkout_custom_heading', 10, 2 );

function XYZ_checkout_custom_heading( $field, $key ){
    if ( is_checkout() && ( $key == 'billing_email') ) {
        $field .= '<div id="add_custom_heading"><h3>' . __('MY CUSTOM HEADING') . '</h3></div>';
    }
    return $field;
}

但是,此操作的结果是对该页面没有任何更改.但是,如果我使用key == 'account_username可以正常工作,除非标题显然位于错误的位置(在帐户用户名字段下方,而不是上方).

But the result of this is no change to the page. However, if I use key == 'account_username it works, except obviously the heading ends up in the wrong place (below the account username field, and not above it).

以下是我指的是视觉上的屏幕截图.

Here is a screen shot of what I am referring to, visually.

推荐答案

如果我正确理解该位置,则该位置位于创建帐户"区域上方.

If I understood it correctly, that location is above "create account" area.

在这种情况下,您可以使用动作挂钩woocommerce_before_checkout_registration_form

If that's the case, you can use action hook woocommerce_before_checkout_registration_form

add_action( 'woocommerce_before_checkout_registration_form','XYZ_checkout_custom_heading');

function XYZ_checkout_custom_heading( ){
    echo '<div id="add_custom_heading"><h3>' . __('MY CUSTOM HEADING') . '</h3></div>';
}

您仍然可以使用woocommerce_form_field_text,但不应该使用$field .= <heading>.但是,请使用$field = <heading> . $field.这样,您的标题将添加到顶部.不在底部.就像field + heading一样,当您执行$field .= <heading>却将heading + field$field = <heading> . $field

You can still use woocommerce_form_field_text but you should not use $field .= <heading>. But instead, use $field = <heading> . $field. This way, your heading is added at the top. Not at the bottom. It's like field + heading when you're doing $field .= <heading> but heading + field with $field = <heading> . $field

add_action( 'woocommerce_form_field_text','XYZ_checkout_custom_heading', 10, 2 );

function XYZ_checkout_custom_heading( $field, $key ){
    if ( is_checkout() && ( $key == 'account_username') ) {
        $field = '<div id="add_custom_heading"><h3>' . __('MY CUSTOM HEADING') . '</h3></div>' . $field;
    }
    return $field;
}

还请注意$key == 'account_username'.

这篇关于如何在Woocommerce中的woocommerce-account-fields上方添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆