Reputation: 25
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'] ) );
}
}
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 );
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.
Am I missing something?
Upvotes: 1
Views: 1148
Reputation: 29650
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.
See: https://businessbloomer.com/woocommerce-add-select-field-account-register-form/
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 );
Upvotes: 1