I’m trying to work on a simple Ajax call and I’m unable to figure out what’s gone wrong. The ajax call is successful but nothing is returning. When I console.log(response), it displays the JSON in my console though. I’m using fullcalendar library and Ajax call not returning a response to the events object
Following is my action function written in functions.php file
function get_order_events() {
$args = array(
'status' => array('processing', 'completed', 'on-hold'),
'orderby' => 'date',
'limit' => -1,
'type' => 'shop_order', );
$customer_orders = wc_get_orders($args); $events = array(); foreach ($customer_orders as $order)
{
foreach ($order->get_items() as $item_id => $item_values) {
$product_id =$item_values['product_id'];
// Get start and end dates from post meta
$start_date = wc_get_order_item_meta($item_id, 'Check-in', true);
$end_date = wc_get_order_item_meta($item_id, 'Checkout', true);
// Add event to the array
$events[] = array(
'title' => get_the_title($product_id).' : Booked',
'start' => $start_date,
'end' => $end_date,
);
}
}
// echo json_encode($events);
wp_send_json_success($events);
wp_die();
// return json_encode($events);
}
add_action('wp_ajax_get_order_events', 'get_order_events'); add_action('wp_ajax_nopriv_get_order_events', 'get_order_events');
Following is my js code
var link = admin_object.ajax_url;
$("#calendar").fullCalendar({
initialView: 'dayGridMonth',
height: 650,
events: function (_info, successCallback, failureCallback) {
jQuery.ajax({
url: link,
type: 'POST', // Use POST method
dataType: 'json',
data: {
action: 'get_order_events'
},
success: function (response) {
console.log(response.data);
successCallback(response.data);
// $('#output').html(response.data);
},
error: function (error) {
console.log("Error");
failureCallback(error);
}
});
}
});