woocommerce_thankyou 在旧订单上触发钩子

问题描述 投票:0回答:1

每当创建新订单时,我都会在 WooCommerce 网站中使用 woocommerce_thankyou 挂钩,通过 POST 请求将订单数据发送到外部 URL。但是,我注意到这个钩子有时会在非常旧的订单上触发,从而导致意外的 POST 请求。这是我正在使用的代码:

add_action('woocommerce_thankyou', 'send_order_to_external_url', 10, 1);
    
function send_order_to_external_url($order_id) {
    if (!$order_id) {
        return;
    }

    // Get the order details
    $order = wc_get_order($order_id);

    // Prepare the data to send
    $data = array(
        'order_id'      => $order_id,
        'order_total'   => $order->get_total(),
        'billing_email' => $order->get_billing_email(),
        'billing_phone' => $order->get_billing_phone(),
        // Add other necessary order details here
    );

    // Convert data to JSON
    $json_data = json_encode($data);

    // The endpoint URL
    $url = 'https://example.com/your-endpoint';

    // Send the data via wp_remote_post
    $response = wp_remote_post($url, array(
        'method'    => 'POST',
        'body'      => $json_data,
        'headers'   => array(
            'Content-Type' => 'application/json',
        ),
    ));

    // Check for errors (optional)
    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        error_log('Error sending order data: ' . $error_message);
    }
}

我的问题是:

  1. 为什么 woocommerce_thankyou 钩子可能会在旧订单上触发?
  2. 如何防止此钩子在除新创建的订单之外的任何内容上触发?

任何调试和解决此问题的见解或建议将不胜感激!

wordpress woocommerce
1个回答
0
投票

每次客户浏览“已收到订单(谢谢)”页面时,都会执行您的功能代码...因此,例如,客户可以保留与他的一个订单相关的“已收到订单(谢谢)”URL,并在另一天再次浏览它.

因此,为了避免出现问题并每个订单仅运行一次代码,请尝试以下替换代码:

add_action('woocommerce_thankyou', 'send_order_to_external_url', 10, 1);
    
function send_order_to_external_url($order_id) {
    if (!$order_id) {
        return;
    }

    // Get the order details
    $order = wc_get_order($order_id);

    // Exit if thankyou page has been already viewed
    if ( $order->get_meta('_thankyou_viewed') ) {
        return;
    }

    // Prepare the data to send
    $data = array(
        'order_id'      => $order_id,
        'order_total'   => $order->get_total(),
        'billing_email' => $order->get_billing_email(),
        'billing_phone' => $order->get_billing_phone(),
        // Add other necessary order details here
    );

    // Convert data to JSON
    $json_data = json_encode($data);

    // The endpoint URL
    $url = 'https://example.com/your-endpoint';

    // Send the data via wp_remote_post
    $response = wp_remote_post($url, array(
        'method'    => 'POST',
        'body'      => $json_data,
        'headers'   => array(
            'Content-Type' => 'application/json',
        ),
    ));

    // Check for errors (optional)
    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        error_log('Error sending order data: ' . $error_message);
    }

    // Tag the order as "thankyou" viewed (add custom metadata)
    $order->update_meta_data('_thankyou_viewed', true);
    $order->save();
}

它应该像以前一样工作,但避免您描述的问题。

© www.soinside.com 2019 - 2024. All rights reserved.