WordPress

Jak wykonać wywołanie Ajax po otrzymaniu zamówienia w WordPress WooCommerce

  • 26 maja, 2023
  • 3 min read
Jak wykonać wywołanie Ajax po otrzymaniu zamówienia w WordPress WooCommerce


Chcę wywołać wywołanie Ajax po otrzymaniu zamówienia. Chcę również dodać, że wywołania Ajax jako rekord bazy danych. Oto mój kod w functions.php plik.

    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="

    $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);
    }
}

Wysyłam również to wywołanie Ajax do pliku api.php, który przypisałem jako ścieżkę szablonu do powyższego adresu URL. Oto kod api.php.

<?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(); ?>

Nie widzę, żeby ten kod działał poprawnie. Czy ktoś może mi pomóc, aby ten kod działał?

Warto przeczytać!  formularze - post Ajax zwracający pełną stronę HTML jako odpowiedź

Z góry dziękuję


Źródło