phpwordpresswoocommercehook-woocommerceorders

WooCommerce order status hook not triggering


I'm using the following function to detect if an order is set into pending. This happens between the payment page and the payment provider notification:

add_action( 'woocommerce_order_status_pending', 'status_pending' );
function status_pending( $related_job ) {
    error_log('Triggered');
}

The problem is that I don't get any error log which shows me that the function work. But it becomes crazier. When I update the status via the dashboard from completed to pending, the log appears. So I've absolutely no idea why it's not working during the checkout process. Any recommendations or ideas what could be the problem?


Solution

  • The "pending" order status is the default status for orders before customer get on a payment gateway, just after order creation.

    So the best way is to use a hook once the order is created, before payment method process:

    1) try first the woocommerce_checkout_order_processed action hook (3 args):

    add_action( 'woocommerce_checkout_order_processed', 'order_processed_with_pending_status', 10, 3 );
    function order_processed_with_pending_status( $order_id, $posted_data, $order ) {
        error_log('Triggered');
    }
    

    2) Alternatively try the woocommerce_checkout_update_order_meta action hook (2 args):

    add_action( 'woocommerce_checkout_update_order_meta', 'order_processed_with_pending_status', 10, 2 );
    function order_processed_with_pending_status( $order_id, $data ) {
        error_log('Triggered');
    }
    

    Both should work…