On the checkout page, on the Billing Details section, I added two custom fields: Alpha and Beta. During checkout these fields are edited by the user and then saved together with the rest of his address info.

On the dashboard, on the "Edit User" page I also added these two custom fields: Alpha and Beta.

On the dashboard when I create an order (Woocommerce > Orders > Add Order) I select a customer. His Billing Details are automatically loaded into the "Billing Details" area.

Now I have to load also the values of the Alpha and Beta custom fields for that specific customer, into the "Custom Fields" area of the "Add New Order" page.

1 - How can I get the ID of that selected customer?

2 - Then, having the user ID how can I get the values of his Alpha and Beta fields and display them into the "Custom Fields" area of the "Add New Order" page?

The Order is not saved yet!

Thank you

When you hover over an user edit profile you can know their ID.

For getting Alpha and Beta value. Since the order is not saved you can't get any value. You need to save the order with Alpha, Beta values through meta boxes input interface.

If you wanted to get the Alpha, Beta values for already created order then you need to get it through

get_post_meta() function with post ID which is order ID in here

  • - It should be done automatically, in PHP code, not manually. - When I select a customer its billing address info is automaticaly displayed. Same should the Alpha and Beta values be displayed. – Valdinia Jan 5 '16 at 14:54
  • Having the selected customer ID I will get the meta values using get_user_meta( $selected_customer_ID, 'alpha', true ); So how do I get the selected customer ID and how do I add alpha and beta values as custom fields in the "Custom Fields" area of the "Add New Order" page? – Valdinia Jan 5 '16 at 15:11

You have to create your custom AJAX call to get and fill these values...

custom-wc-order-meta.js

/*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order */
jQuery( function ( $ ) {
    var custom_wc_meta_boxes_order = {
        init: function() {
            $( 'a.load_customer_billing' ).on( 'click', this.load_billing );
            $( '#customer_user' ).on( 'change', this.change_customer_user );
        },

        change_customer_user: function() {
            custom_wc_meta_boxes_order.load_billing( true );
        },

        load_billing: function( force ) {
            if ( true === force || window.confirm( woocommerce_admin_meta_boxes.load_billing ) ) {

                // Get user ID to load data for
                var user_id = $( '#customer_user' ).val();

                if ( ! user_id ) {
                    window.alert( woocommerce_admin_meta_boxes.no_customer_selected );
                    return false;
                }

                var data = {
                    user_id:      user_id,
                    type_to_load: 'billing',
                    action:       'custom_woocommerce_get_customer_details',
                    security:     woocommerce_admin_meta_boxes.get_customer_details_nonce
                };

                $( this ).closest( 'div.edit_address' ).block({
                    message: null,
                    overlayCSS: {
                        background: '#fff',
                        opacity: 0.6
                    }
                });

                $.ajax({
                    url: woocommerce_admin_meta_boxes.ajax_url,
                    data: data,
                    type: 'POST',
                    success: function( response ) {
                        var info = response;

                        if ( info ) {
                            $( 'input#_billing_alpha' ).val( info.billing_alpha ).change();
                            $( 'input#_billing_beta' ).val( info.billing_beta ).change();
                        }

                        $( 'div.edit_address' ).unblock();
                    }
                });
            }
            return false;
        },
    };

    custom_wc_meta_boxes_order.init();
});

custom-woocommerce-admin.php

//admin enqueue scripts handler
add_action( 'admin_enqueue_scripts', 'custom_woocommerce_admin_scripts', 20 );

function custom_woocommerce_admin_scripts() {
    wp_enqueue_script( 'custom_woocommerce_admin', '/path/to/custom-wc-order-meta.js', array( 'wc-admin-meta-boxes' ), '0.0.1' );
}

// add WP admin AJAX handler    
add_action( 'wp_ajax_custom_woocommerce_get_customer_details' , 'custom_woocommerce_get_customer_details' );

/**
 * Get customer details via ajax
 */
function custom_woocommerce_get_customer_details() {
    ob_start();

    check_ajax_referer( 'get-customer-details', 'security' );

    if ( ! current_user_can( 'edit_shop_orders' ) ) {
        die(-1);
    }

    $user_id      = (int) trim(stripslashes($_POST['user_id']));
    $type_to_load = esc_attr(trim(stripslashes($_POST['type_to_load'])));

    $customer_data = array(
        $type_to_load . '_alpha' => get_user_meta( $user_id, $type_to_load . '_alpha', true ),
        $type_to_load . '_beta'  => get_user_meta( $user_id, $type_to_load . '_beta', true ),
    );

    $customer_data = apply_filters( 'custom_woocommerce_found_customer_details', $customer_data, $user_id, $type_to_load );

    wp_send_json( $customer_data );
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.