How to do a Ajax call once an order is received in WordPress WooCommerce
Question
I want to trigger an Ajax call when I received an order. Also, I want to add that Ajax calls as a database record. Here is my code in the functions.php
file.
add_action('woocommerce_before_checkout_process', 'send_api_call_before_checkout_process');
function send_api_call_before_checkout_process() {
if (is_user_logged_in()) {
$order_id = wc_get_order($order_id);
send_api_call($order_id, 'Order Received');
// Prevent redirection
add_filter('woocommerce_checkout_no_payment_needed_redirect', '__return_true');
}
}
function send_api_call($order_id, $order_status) {
echo 'API Called';
error_log('send_api_call function triggered.');
error_log('Order ID: ' . $order_id);
error_log('Order Status: ' . $order_status);
$order = wc_get_order($order_id);
$api_url="https://mywebsite.com/api-call/";
$data = array(
'order_id' => $order->get_id(),
'first_name' => $order->get_billing_first_name(),
'last_name' => $order->get_billing_last_name(),
'email_address' => $order->get_billing_email(),
'product_name' => '', // Replace with the appropriate product name
'product_sku' => '', // Replace with the appropriate product SKU
'order_status' => $order_status, // Use the provided order status
'regular_price' => '', // Replace with the regular price
'selling_price' => '', // Replace with the selling price
);
$response = wp_remote_post($api_url, array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => json_encode($data),
));
if (is_wp_error($response)) {
error_log('API call failed: ' . $response->get_error_message());
} else {
$response_code = wp_remote_retrieve_response_code($response);
$response_body = wp_remote_retrieve_body($response);
error_log('API call successful: Response Code - ' . $response_code . ', Body - ' . $response_body);
}
}
Also, I am sending this Ajax call to the api.php file which I have assigned as a template path to the above URL. Here is the api.php code.
<?php
/*
Template Name: API Call
*/
get_header();
?>
<h1>Order Check</h1>
<?php
require_once 'functions.php';
function insert_data_to_database($order_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'woo_api_call';
$order = wc_get_order($order_id);
// print_r($order);
$data = array(
'order_id' => $order->get_id(),
'first_name' => $order->get_billing_first_name(),
'last_name' => $order->get_billing_last_name(),
'email_address' => $order->get_billing_email(),
'product_name' => '', // Replace with the appropriate product name
'product_sku' => '', // Replace with the appropriate product SKU
'order_status' => 'processing', // Change the status based on your requirement
'regular_price' => '', // Replace with the regular price
'selling_price' => '', // Replace with the selling price
'timestamp' => current_time('mysql'),
'status' => 1, // Assuming 'status' field is a Boolean value (1 for success)
);
$wpdb->insert($table_name, $data);
if ($wpdb->insert_id !== false) {
$success_message="Data inserted successfully.";
error_log($success_message);
} else {
$error_message="Failed to insert data into the database.";
error_log($error_message);
}
}
if (isset($_POST['order_id'])) {
$order_id = $_POST['order_id'];
insert_data_to_database($order_id);
$response = array(
'status' => 'success',
'message' => 'Data inserted successfully.',
);
echo json_encode($response);
} else {
$response = array(
'status' => 'error',
'message' => 'Invalid request.',
);
echo json_encode($response);
}
?>
<?php get_footer(); ?>
I can’t see this code is working properly. Can anybody assist me to make this code work?
Thanks in advance
0
1 week
2023-05-25T23:29:30-05:00
2023-05-25T23:29:30-05:00 0 Answers
0 views
0
Leave an answer