我想从另一个通过wp_ajax调用的操作中调用Woocommerce本地操作。但是原生的Woocommerce操作无法正常工作。尽管如果我在wp_ajax动作之外调用Woocommerce,就可以正常工作
wp_ajax操作从选择输入更改中获取一个值(显然,我使用js获取了该值),并将其传递给wp_ajax操作回调函数中的另一个操作
$('#delivery_time_field_name').on('change', function(e) {
$.ajax({
url: ajax_object.ajaxurl,
type: 'post',
data: {
_ajax_nonce: ajax_object.ajax_nonce,
action: "update_order_review",
timeData: $( this ).val()
},
success: function(response) {
if (response.success) {
console.log(response.data)
$( 'body' ).trigger( 'update_checkout' );
}
}
});
});
add_action( 'wp_ajax_nopriv_update_order_review', 'update_order_review' );
add_action( 'wp_ajax_update_order_review', 'update_order_review' );
function update_order_review() {
$time_data = $_POST['timeData'];
add_action( 'woocommerce_cart_calculate_fees', function ($cart) use ($time_data) {
add_custom_fee($cart, $time_data);
}, 10, 2);
wp_send_json_success($time_data);
}
function add_custom_fee ( $cart, $time_data ) {
if(is_checkout()){
$time_slot_fee = 3;
if($time_data == "450,480") {
$time_slot_fee = 10;
} else {
$time_slot_fee = 20;
}
$delivery_date_fee = 10;
$cart->add_fee( __( 'Delivery Date Fee', 'demo-plugin' ) , $delivery_date_fee, false );
$cart->add_fee( __( 'Delivery Time Slot Fee', 'demo-plugin' ) , $time_slot_fee, false );
}
}
没有错误消息