在 WooCommerce 管理订单编辑页面中显示帐单地址之间的自定义字段 [英] Display custom field between billing address in WooCommerce admin order edit pages
问题描述
我目前正在尝试将自定义字段添加到默认地址字段(名字、姓氏等...)
I´m currently trying to add a custom field to the default address fields (firstname, lastname, etc...)
应该使用该字段来为客户设置称呼.
The field should be used, to set a salutation for the customer.
为此,我使用了以下过滤器:
add_filter( 'woocommerce_default_address_fields', 'custom_woocommerce_address_fields' );
function custom_woocommerce_address_fields($fields) {
$fields['salutation'] = array(
'label' => __('Anrede', 'woocommerce'), // Add custom field label
'placeholder' => 'CA12345678', // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'select', // add field type
'options' => array(
'Herr' => 'Herr',
'Frau' => 'Frau',
'Firma' => 'Firma'
),
'priority' => 1, // add priority
'class' => array('billing-salutation-input')// add class name
);
return $fields;
}
保存自定义字段:
add_action( 'woocommerce_checkout_update_order_meta', 'save_new_checkout_field' );
function save_new_checkout_field( $order_id ) {
if ( $_POST['billing_salutation'] ) update_post_meta( $order_id, '_salutation', esc_attr( $_POST['billing_salutation'] ) );
}
问题:
当我查看订单详细信息时,默认地址字段下不会显示新字段.
When I look at the order details, the new field won´t show up under the default address fields.
所以我用下面的代码来显示新数据.
So I used the following code to show the new data.
add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_new_checkout_field_order', 10, 1 );
function show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_salutation', true ) ) echo '<p><strong>Anrede:</strong> ' . get_post_meta( $order_id, '_salutation', true ) . '</p>';
}
但是,字段总是显示在底部,我找不到将字段带到所需位置(图像)的方法
However, the field is always displayed at the bottom and I can't find a way to bring the field to the desired position (image)
我是否必须使用不同的钩子来显示所需位置的数据(帐单明细)?
Do I have to use a different hook to display the data at the desired location (billing details)?
或者我必须编辑特定的 woocommerce 模板吗?
Or do I have to edit a specifc woocommerce template?
推荐答案
输出是一个字符串,使用<br/>
分隔订单账单地址详情.
The output is a string, which uses <br/>
to separate the order billing address details.
所以有两种可能的视图:
So there are 2 possible views:
company_name
Firstname Lastname
street
1
1000 位置Firstname Lastname
street
1
1000 Location
您可以应用多种可能性来回答您的问题,但最简单的方法是通过 woocommerce_order_get_formatted_billing_address
过滤器钩子编辑字符串.
There are several possibilities that you can apply to answer your question, but the easiest is to edit the string via the woocommerce_order_get_formatted_billing_address
filter hook.
注意:我们要检查是否真的有计费公司,并在此基础上调整我们的结果.
Note: we are going to check whether there is actually a billing company, and adjust our result based on that.
注意:根据您的需要调整$order->get_meta('_your_meta_key');
.
所以你得到:
function str_replace_first( $search, $replace, $subject ) {
$pos = strpos( $subject, $search );
if ( $pos !== false ) {
return substr_replace( $subject, $replace, $pos, strlen( $search ) );
}
return $subject;
}
/**
* Filter orders formatterd billing address.
*
* @since 3.8.0
* @param string $address Formatted billing address string.
* @param array $raw_address Raw billing address.
* @param WC_Order $order Order data. @since 3.9.0
*/
function filter_woocommerce_order_get_formatted_billing_address( $address, $raw_address, $order ) {
// Get meta
$value = $order->get_meta( '_your_meta_key' );
// When empty, return
if ( empty ( $value ) ) return $address;
// Get billing company
$billing_company = $order->get_billing_company();
// Not empty
if ( ! empty ( $billing_company ) ) {
$address = str_replace_first( '<br/>', '<br/>' . $value . '<br/>', $address );
} else {
$address = $value . '<br/>' . $address;
}
return $address;
}
add_filter( 'woocommerce_order_get_formatted_billing_address', 'filter_woocommerce_order_get_formatted_billing_address', 10, 3 );
结果:
company_name
meta_value
Firstname Lastname
street
1
1000 Locationmeta_value
Firstname Lastname
street
1
1000 Location
在此答案中使用:https://stackoverflow.com/a/2606638/11987538
这篇关于在 WooCommerce 管理订单编辑页面中显示帐单地址之间的自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!