重新排序 Woocommerce 结帐字段 [英] Reorder Woocommerce checkout fields
问题描述
我正在尝试对结帐页面上的帐单字段重新排序,但到目前为止我尝试的所有方法都不起作用.
I am trying to reorder the billing fields on the checkout page but everything I tried so far is not working.
这是我目前正在尝试的片段:
Here is the snippet I am currently trying:
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"billing_first_name",
"billing_last_name",
"billing_country",
"billing_state",
"billing_address_1",
"billing_address_2",
"billing_email",
"billing_phone"
);
foreach($order as $field) {
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
有什么东西可以覆盖这个片段,因为它曾经对我有用.
Could something be overriding this snippet, because it used to work for me.
推荐答案
您使用的方法不再有效(自 WooCommerce 3.0.4 更新 - 此处链接到 github 问题).这不完全是错误,而是规范的更改.
The method you are using no longer works (since the WooCommerce 3.0.4 update - link to github issue here). It's not exactly a bug, but rather a change of spec.
要解决此问题,您只需调整每个字段的优先级以适合您想要的顺序.
To fix this, you simply need to adjust the priority of each of the fields to fit the order that you want.
add_filter("woocommerce_checkout_fields", "custom_override_checkout_fields", 1);
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_first_name']['priority'] = 1;
$fields['billing']['billing_last_name']['priority'] = 2;
$fields['billing']['billing_company']['priority'] = 3;
$fields['billing']['billing_country']['priority'] = 4;
$fields['billing']['billing_state']['priority'] = 5;
$fields['billing']['billing_address_1']['priority'] = 6;
$fields['billing']['billing_address_2']['priority'] = 7;
$fields['billing']['billing_city']['priority'] = 8;
$fields['billing']['billing_postcode']['priority'] = 9;
$fields['billing']['billing_email']['priority'] = 10;
$fields['billing']['billing_phone']['priority'] = 11;
return $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;
}
请注意,不是代码中的行调整顺序中的位置,而是分配给字段的优先级.在这种情况下,我将 ['billing_state']
优先级更改为 5
,即(在本例中)在 ['billing_country']
和 ['billing_address_1']
之前.
Please note that it's not the line in code that adjusts the placement in the order, but the priority assigned to the field. In this case, I changed the ['billing_state']
priority to 5
which is (in this example) after ['billing_country']
and before ['billing_address_1']
.
根据评论,这由于某种原因不起作用(它在我的第一个 WooCommerce 安装测试中起作用,但在更改商店位置后我设法让它失败).
As per the comments, this wasn't working for some reason (it worked on my first test WooCommerce installation, but I managed to get it to fail after changing the store location).
经过一番挖掘,我发现默认区域设置应用在覆盖之上(不知道如何解释这一点,但请耐心等待).
After some digging, I found that the default locale settings get applied on top of the override (not sure how else to explain this, but bear with me).
我设法在上述代码不起作用的情况下运行安装,并通过添加以下代码设法修复它:
I managed to get an install running where the above code was not working, and managed to fix it by adding the following code as well:
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;
}
我已将额外的代码段添加到第一个代码块以验证整个块.
I have added the extra snippet to the first code block to validate the entire block.
这篇关于重新排序 Woocommerce 结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!