在WooCommerce订单管理员列表上显示带有自定义用户元的自定义列 [英] Display custom column with custom user meta on WooCommerce order admin list
问题描述
有点儿困惑.我已经尝试了几天,以获取我从WooCommerce注册表单中的自定义字段收集的数据,并将其插入数据库中的用户meta中.表单的代码为:
In a bit of a quandary here. I've been trying for a couple of days now to get my data, that I collected from a custom field in WooCommerce registration form to insert into user meta in database. The code for the form is:
//Addition of morello Account Number to reg page
add_action( 'woocommerce_register_form', 'add_register_form_field' );
function add_register_form_field(){
woocommerce_form_field(
'morello_account_number',
array(
'type' => 'text',
'required' => true, // required field?
'label' => 'morello Account Number:'
),
( isset($_POST['morello_account_number']) ? $_POST['morello_account_number'] : '' )
);
}
add_action( 'woocommerce_created_customer', 'save_register_fields' );
function save_register_fields( $customer_id ){
if ( isset( $_POST['morello_account_number'] ) ) {
update_user_meta( $customer_id, 'morello_account_number', wc_clean( $_POST['morello_account_number'] ) );
}
}
现在,我想从表中获取数据并将其显示在WooCommerce的订单"页面上的单独列中,这样便可以查看莫雷洛编号,而不必手动搜索客户详细信息.
Now I want to grab the data from the table and display it on the Order page of WooCommerce in a separate column, so I can then see the morello number without having to search customer details manually.
到目前为止,这是我的代码:
Here's my code so far for this:
/**
* Add columns
*/
function morello_account_number_column( $columns ) {
$columns['morello_account_number'] = "morello Account Number";
return $columns;
}
add_filter('manage_edit-shop_order_columns', 'morello_account_number_column', 10, 1 );
/**
* Populate columns
*/
function morello_placeholder( $column, $post_id ) {
if( $column == 'morello_account_number' ) {
// https://developer.wordpress.org/reference/functions/get_post_meta/
$a_a_n = get_user_meta( $user_id, 'morello_account_number', true );
// Value is found
if ( !empty($a_a_n) ) {
echo $a_a_n;
} else {
echo 'something else';
}
}
}
add_filter( 'manage_shop_order_posts_custom_column', 'morello_placeholder', 10, 2 );
这可以按预期工作,但会一直显示其他"信息. quote-代码将在正确的位置创建该列,但不会在morello_account_number中填充该列.
This works as intended, but keeps displaying the "something else" quote - the code creates the column in the correct place, but won't populate it with the morello_account_number.
我想念什么吗?
推荐答案
您的代码中有一些小错误.
There are some small mistakes in your code.
注意:请注意,如果将新字段添加到注册表单中,最好在多个位置返回此字段,以便用户随后进行调整.
NOTE: Be aware that if you add a new field to the registration form, it is best to have this field returned in several places, so that the user can adjust it afterwards.
请参阅: https://businessbloomer.com/woocommerce- add-select-field-account-register-form/
第3部分:显示字段 @用户个人资料(管理员)和我的帐户编辑页面(前端)
Part 3: Display Field @ User Profile (admin) and My Account Edit page (front end)
所以你得到
So you get
/**
* Add morello account number to reg page
*/
function add_register_form_field() {
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_morello_account_number"><?php _e( 'Morello Account Number', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="morello_account_number" id="reg_morello_account_number" value="<?php if ( ! empty( $_POST['morello_account_number'] ) ) echo esc_attr( $_POST['morello_account_number'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form', 'add_register_form_field' );
/**
* Save morello account number
*/
function save_register_fields( $customer_id, $new_customer_data, $password_generated ) {
if ( isset( $_POST['morello_account_number'] ) ) {
update_user_meta( $customer_id, 'morello_account_number', wc_clean( $_POST['morello_account_number'] ) );
}
}
add_action( 'woocommerce_created_customer', 'save_register_fields', 10 , 3 );
/**
* Add columns
*/
function morello_account_number_column( $columns ) {
$columns['morello_account_number'] = "morello Account Number";
return $columns;
}
add_filter('manage_edit-shop_order_columns', 'morello_account_number_column', 10, 1 );
/**
* Populate columns
*/
function morello_placeholder( $column, $post_id ) {
// Get order
$order = wc_get_order( $post_id );
// Get user id
$user_id = $order->get_user_id();
if( $column == 'morello_account_number' ) {
$a_a_n = get_user_meta( $user_id, 'morello_account_number', true );
// Value is found
if ( !empty($a_a_n) ) {
echo $a_a_n;
} else {
echo 'something else';
}
}
}
add_filter( 'manage_shop_order_posts_custom_column', 'morello_placeholder', 10, 2 );
这篇关于在WooCommerce订单管理员列表上显示带有自定义用户元的自定义列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!