Avoid woocommerce_thankyou hook to be trigerred multiple times or on old orders

I’m using the woocommerce_thankyou hook in my WooCommerce site to send order data to an external URL via a POST request whenever a new order is created. However, I’ve noticed that this hook is sometimes triggering on very old orders, causing unexpected POST requests. Here’s the code I’m using:

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

My questions are:

  1. Why might the woocommerce_thankyou hook be triggering on old orders?
  2. How can I prevent this hook from triggering on anything but newly created orders?

Any insights or suggestions to debug and fix this issue would be greatly appreciated!

Each time a customer browse the Order received (thankyou) page, your function code is executed… So for example, a customer can keep the Order received (thankyou) URL related to one of his orders, and browse it again another day, later.

So to avoid your issue you can:

  • force the code to be executed only once per order,
  • avoid orders with a creation date older than 3 days (for example).

Try the following replacement code:

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

    // Exit if the order creation date is older than "3 days"
    if ( strtotime($order->get_date_created()) < strtotime('- 3 days') ) {
        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();
}

It should work as before, but avoiding the issue you describe.

You can change easily the allowed number of days related to the date creation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật