How to send API call once an order received
-
I want to send an API call to a third-party URL outside the website domain when an order is received. Therefore I tried this code to send the details I required to send.
// Register a custom AJAX endpoint add_action('wp_ajax_process_order_received', 'process_order_received'); add_action('wp_ajax_nopriv_process_order_received', 'process_order_received'); function process_order_received() { if (isset($_POST['order_id'])) { $order_id = intval($_POST['order_id']); // Get the order object $order = wc_get_order($order_id); // Get the customer details $first_name = $order->get_billing_first_name(); $last_name = $order->get_billing_last_name(); $email_address = $order->get_billing_email(); // Get the product details $items = $order->get_items(); $item = reset($items); $product = $item->get_product(); $product_name = $product->get_name(); $product_sku = $product->get_sku(); // Get the order status $order_status = $order->get_status(); // Get the order totals $regular_price = $order->get_total(); $selling_price = $order->get_total(); // Log the data for debugging purposes error_log('Order ID: ' . $order_id); error_log('First Name: ' . $first_name); error_log('Last Name: ' . $last_name); error_log('Email Address: ' . $email_address); error_log('Product Name: ' . $product_name); error_log('Product SKU: ' . $product_sku); error_log('Order Status: ' . $order_status); error_log('Regular Price: ' . $regular_price); error_log('Selling Price: ' . $selling_price); // Send AJAX API call $api_url = 'https://thirdpartywebsite/api/api.php'; $data = array( 'order_id' => $order_id, 'first_name' => $first_name, 'last_name' => $last_name, 'email_address' => $email_address, 'product_name' => $product_name, 'product_sku' => $product_sku, 'order_status' => $order_status, 'regular_price' => $regular_price, 'selling_price' => $selling_price, ); $response = wp_remote_post($api_url, array( 'body' => $data, 'timeout' => 45, 'sslverify' => true, )); if (!is_wp_error($response)) { // Handle the API response if needed $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); // Process the response as required // Log the API response for debugging purposes error_log('API Response Code: ' . $response_code); error_log('API Response Body: ' . $response_body); } } // Always die or exit at the end of an AJAX callback die(); }
And the ajax function
add_action('woocommerce_checkout_update_order_meta', 'send_order_details_ajax'); function send_order_details_ajax($order_id) { // Get the order ID from $_POST if (isset($_POST['order_id'])) { $order_id = $_POST['order_id']; } // Get the order object $order = wc_get_order($order_id); // Get the customer details $first_name = $order->get_billing_first_name(); $last_name = $order->get_billing_last_name(); $email_address = $order->get_billing_email(); // Get the product details $items = $order->get_items(); $item = reset($items); // Assuming there is only one item in the order $product = $item->get_product(); $product_name = $product->get_name(); $product_sku = $product->get_sku(); // Get the order status $order_status = $order->get_status(); // Get the order totals $regular_price = $order->get_total(); $selling_price = $order->get_total(); // Send AJAX request $data = array( 'order_id' => $order_id, 'first_name' => $first_name, 'last_name' => $last_name, 'email_address' => $email_address, 'product_name' => $product_name, 'product_sku' => $product_sku, 'order_status' => $order_status, 'regular_price' => $regular_price, 'selling_price' => $selling_price, ); wp_send_json($data); }
This when we tried to
echo json_encode($data);
the data getting output as NULL.How to manage this in WooCommerce, please?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to send API call once an order received’ is closed to new replies.