通过 AJAX 获取 Woocommerce 客户详细信息并将其填充到 Gravity Forms 表单
Get Woocommerce customer details by AJAX and populate it to Gravity Forms form
我需要获取客户详细信息(个人资料页面上提供的客户数据)并在需要时将其填充到 Gravity Form 表单中。
到目前为止,我意识到我需要像 here 描述的那样的东西,但由于某些原因,我的 javascript 没有按预期返回数据。这是我的代码:
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 ');
}
});
}
});
这是我单击复选框 (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
我认为问题出在我注释掉的那一行:
// security: woocommerce_admin_meta_boxes.get_customer_details_nonce
我这样做是因为它遇到了错误:
Uncaught ReferenceError: woocommerce_admin_meta_boxes is not defined
我在 Woocommerce 代码中看到它在少数地方使用了 woocommerce_admin_meta_boxes 对象(例如在 woocommerce\assets\js\admin\meta-boxes-order.js:119 中),但出于某种原因我不能。
如有任何帮助,我们将不胜感激。
好的,解决方案非常简单 - 在 PHP 方面,我只需要使用 wp_create_nonce() 创建并将其作为变量添加到 javascript:
$globals = array(
'userID' => $userID,
'get_customer_details_nonce' => wp_create_nonce('get-customer-details')
);
wp_localize_script( 'wpchris-customization-js', 'globals', $globals );
现在,在 Javascript 部分我可以使用这个随机数:
var data = {
user_id: globals.userID,
type_to_load: 'billing',
action: 'woocommerce_get_customer_details',
security: globals.get_customer_details_nonce
};
我需要获取客户详细信息(个人资料页面上提供的客户数据)并在需要时将其填充到 Gravity Form 表单中。
到目前为止,我意识到我需要像 here 描述的那样的东西,但由于某些原因,我的 javascript 没有按预期返回数据。这是我的代码:
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 ');
}
});
}
});
这是我单击复选框 (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
我认为问题出在我注释掉的那一行:
// security: woocommerce_admin_meta_boxes.get_customer_details_nonce
我这样做是因为它遇到了错误:
Uncaught ReferenceError: woocommerce_admin_meta_boxes is not defined
我在 Woocommerce 代码中看到它在少数地方使用了 woocommerce_admin_meta_boxes 对象(例如在 woocommerce\assets\js\admin\meta-boxes-order.js:119 中),但出于某种原因我不能。
如有任何帮助,我们将不胜感激。
好的,解决方案非常简单 - 在 PHP 方面,我只需要使用 wp_create_nonce() 创建并将其作为变量添加到 javascript:
$globals = array(
'userID' => $userID,
'get_customer_details_nonce' => wp_create_nonce('get-customer-details')
);
wp_localize_script( 'wpchris-customization-js', 'globals', $globals );
现在,在 Javascript 部分我可以使用这个随机数:
var data = {
user_id: globals.userID,
type_to_load: 'billing',
action: 'woocommerce_get_customer_details',
security: globals.get_customer_details_nonce
};