我尝试重新订购在woocommerce_checkout_init
过滤器的帮助下添加的2个自定义结帐字段,只是当我应用woocommerce_checkout_fields
过滤器重新排序字段时,它没有&#39 ; t识别他们,他们是null
我认为这是因为过滤器woocommerce_checkout_init
位于woocommerce_checkout_fields
之后。
我该如何解决这个问题?
这是我的代码:
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_email_checkout', 10, 2 );
function wc_add_confirm_email_checkout( $checkout ) {
$checkout->checkout_fields['billing']['billing_email2'] = array(
'type' => 'text',
'label' => __( 'Confirm Email Address', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Email Address', 'placeholder', 'woocommerce' )
);
}
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 2 );
function wc_add_confirm_password_checkout( $checkout ) {
//var_dump($checkout->checkout_fields);
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout->checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
}
add_filter('woocommerce_checkout_fields','reorder_woo_fields');
function reorder_woo_fields($fields) {
$fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
$fields2['billing']['billing_last_name'] = $fields['billing']['billing_last_name'];
$fields2['billing']['billingooglg_email'] = $fields['billing']['billing_email'];
$fields2['billing']['billing_email2'] = $fields['billing']['billing_email2'];
$fields2['billing']['account_password'] = $fields['account']['account_password'];
$fields2['billing']['account_password2'] = $fields['account']['account_password2'];
$fields2['billing']['billing_address_1'] = $fields['billing']['billing_address_1'];
$fields2['billing']['billing_postcode'] = $fields['billing']['billing_postcode'];
var_dump($fields2);
//return $fields2;
}
答案 0 :(得分:2)
适用于WooCommerce 3+ (更新):
由于 WooCommerce 3.0结帐字段稍有变化,因此无法像以前那样重新排序字段。
对于结帐字段和我的帐户字段,还有一个新的' 参数处理字段顺序。
以下是WooCommerce 3 +的完整功能示例:
// REORDERING CHECKOUT BILLING FIELDS (WOOCOMMERCE 3+)
add_filter( "woocommerce_checkout_fields", "reordering_checkout_fields", 15, 1 );
function reordering_checkout_fields( $fields ) {
## ---- 1. REORDERING BILLING FIELDS ---- ##
// Set the order of the fields
$billing_order = array(
'billing_first_name',
'billing_last_name',
'billing_email',
'billing_phone',
'billing_company',
'billing_address_1',
'billing_address_2',
'billing_postcode',
'billing_city',
'billing_state',
'billing_country'
);
$count = 0;
$priority = 10;
// Updating the 'priority' argument
foreach($billing_order as $field_name){
$count++;
$fields['billing'][$field_name]['priority'] = $count * $priority;
}
## ---- 2. CHANGING SOME CLASSES FOR BILLING FIELDS ---- ##
$fields['billing']['billing_email']['class'] = array('form-row-first');
$fields['billing']['billing_phone']['class'] = array('form-row-last');
$fields['billing']['billing_postcode']['class'] = array('form-row-first');
$fields['billing']['billing_city']['class'] = array('form-row-last');
## ---- RETURN THE BILLING FIELDS CUSTOMIZED ---- ##
return $fields;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
在WooCommerce 3之前
我不完全确定,但是您有一些事情可以做,例如将结算字段与帐户字段合并。如果你想这样做会比你想要做的要复杂得多。在这种情况下,您需要重写/创建一些结帐模板......
另一件事是 billing_email
和 billing_phone
与'class' => 'form-row-first'
和'class' => 'form-row-last'
共享同一行。 不时,此类定义 'class' => 'form-row-wide'
...所以您还需要覆盖这些 'class'
。
之后你不需要使用 'woocommerce_checkout_init'
挂钩...
您仍然可以使用 'woocommerce_checkout_fields'
。
您也可以通过以下方式将所有这些功能合并到一个功能中:
/*
* Creating, overriding and reordering custom fields.
*/
add_filter( "woocommerce_checkout_fields", "custom_override_checkout_fields", 11, 1 );
function custom_override_checkout_fields( $fields ) {
// Creating 'billing_email2' field
$fields['billing']['billing_email2'] = array(
'type' => 'text',
'label' => __( 'Confirm Email Address', 'woocommerce' ),
'placeholder' => _x( 'Confirm Email Address', 'placeholder', 'woocommerce' ),
'required' => true,
'class' => array('form-row-last'),
'clear' => true
);
// =======> I don't really know if you need this one <========
// it already exist (see in first reference link at bottom).
// Creating 'account_password2' field
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' ),
'required' => true,
'class' => array('form-row-wide') //,
// 'clear' => true
);
}
// Overriding existing billing_phone field 'class' property
$fields['billing']['billing_phone']['class'] = array('form-row-wide');
// Reordering billing fields
$order = array(
"billing_first_name",
"billing_last_name",
"billing_email",
"billing_email2",
"billing_phone",
"billing_company",
"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;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
正如我之前所说,我认为您无法将结算字段与帐户字段合并
由于 'account_password2'
已经存在,如果您参考官方文档(请参阅下面的第一个参考链接),您可能不需要创建它。你将不得不测试它并进行微调。但这是做到这一点的方法。
参考文献:
答案 1 :(得分:1)
<强>注意!强>
Woocommerce已经做了一些更改,为了安排地址字段,您应该使用'woocommerce_default_address_fields'过滤器。
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_locale_fields' );
function custom_override_default_locale_fields( $fields ) {
$fields['state']['priority'] = 5;
$fields['address_1']['priority'] = 6;
$fields['address_2']['priority'] = 7;
return $fields;
}
答案 2 :(得分:1)
要添加到接受的答案...
如果您需要将表单字段从一个字段组移动到另一字段组,例如将运输字段移动到开票或将帐户字段移动到开票,则可以执行以下操作。
function move_password_field($checkout_fields){
// Move Account Password into Billing
$checkout_fields['billing']['account_password'] = $checkout_fields['account']['account_password'];
// Remove Password from Billing
unset($checkout_fields['account']['account_password']);
return $checkout_fields;
}
add_filter('woocommerce_checkout_fields', 'move_password_field', 999);