'Get Woocommerce customer details by AJAX and populate it to Gravity Forms form

I need to get customer details (customer data available on a profile page) and populate it on Gravity Form form when needed.

So far I realized I need something like described here, but for some reason my javascript is not returning data as expected. Here is my code:

jQuery(document).ready(function() {
    // form pre-population when "I am the Insured* entity" checkbox is checked
    if ( !globals.logged_in ) { 
        jQuery('li#field_1_160').remove();
    } else {
        jQuery('#choice_1_160_1').change(function() {
            console.log('customer data: change ');
            if(jQuery(this).is(":checked")) {
                console.log('customer data: checked ');

                var data = {
                    user_id:      globals.userID,
                    type_to_load: 'billing',
                    action:       'woocommerce_get_customer_details',
                    // security:     woocommerce_admin_meta_boxes.get_customer_details_nonce
                };

                jQuery.ajax({
                    url: globals.ajax_url,
                    data: data,
                    type: 'POST',
                    success: function( response ) {
                        var info = response;
                        console.log('customer data: ' + response);
                    },
                    error: function() {
                        console.log('An error occurred');
                    }
                    });
            } else {
                console.log('customer data: unchecked ');
            }
        });
    } 
});

And here is what I get in console when I click the checkbox (id choice_1_160_1):

wpchris.com.js?ver=1.0:68 customer data: change 
wpchris.com.js?ver=1.0:71 customer data: checked 
wpchris.com.js?ver=1.0:87 customer data: -1

I believe the problem is somewhere with the line I commented out:

// security:     woocommerce_admin_meta_boxes.get_customer_details_nonce

and I did this because it runs into an error:

Uncaught ReferenceError: woocommerce_admin_meta_boxes is not defined

I saw in the Woocommerce code that it uses woocommerce_admin_meta_boxes object in few places (for example in woocommerce\assets\js\admin\meta-boxes-order.js:119) but for some reason I can't.

Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source