【发布时间】:2019-03-11 03:25:42
【问题描述】:
这个问题会让我很快就去邮局……
在 Woocommerce Checkout 中,我需要在地址中添加一个自定义字段。
这个额外的字段用于function calculate_shipping( $package = array())
现在显然 woocommerce / wordpress 不允许访问此函数调用中的这些自定义字段。不明白为什么,但我们继续前进。..
所以解决方案应该是 jQuery 吧? ..没那么快.. 每个示例都不会将值返回到 woocommerce 环境中......不是一个......
继续我到目前为止在这个兔子洞中寻找单个字段值的代码......
//We first add the field to the checkout form
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields');
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_area'] = array(
'label' => __('Area', 'woocommerce'),
'placeholder' => _x('Area', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide' ),
'clear' => true,
'priority' => 61
);
return $fields;
}
// we load the scripts and the ajax callback functions
add_action( 'wp_enqueue_scripts', 'so18550905_enqueue_scripts' );
add_action( 'wp_ajax_myaction', 'so18550905_wp_ajax_function' );
add_action( 'wp_ajax_nopriv_myaction' , 'so18550905_wp_ajax_function' );
function so18550905_enqueue_scripts(){
wp_register_script( 'ajaxHandle', plugins_url() . '/miguel-shipping/test.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'ajaxHandle' );
wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );
}
function so18550905_wp_ajax_function(){
//this function should be called but it is not for some reason..
$testingitouthere=$_POST['my_billing_area'] ;
file_put_contents('blablablabla.txt', print_r($testingitouthere,true));
wp_die();
}
JS 代码:
jQuery(document).ready( function($){
$('#billing_area').change(function () {
var billing_area = $('#billing_area').val();
console.log(billing_area);
$.ajax({
url: ajax_object.ajaxurl, // this is the object instantiated in wp_localize_script function
type: 'POST',
data:{
action: 'myaction',
my_billing_area: billing_area
},
success: function( data ){
//Do something with the result from server
console.log( data );
}
});
return false;
});
});
问题是php中调用的函数
so18550905_wp_ajax_function()
只是永远不会被触发..我正在写入该函数中的文件(在有人询问之前)仅用于测试..一旦我到达那一点,我将从那里继续编码...... Jquery 也正在登录控制台..
【问题讨论】:
标签: php jquery ajax wordpress woocommerce