Skip to content
Advertisement

WooCommerce successful order hook

I am trying to send an email on success order so I was using until now the woocommerce_thankyou hook which seems to work perfect. But I found out that it is triggerred even when a customer tries to pay to external payment gateway (credit card payment), even though the payment is not accepted by the bank.

Which hook can I use in order to cover all these cases?

Bank Transfer, COD, Credit cart (only in succesfull payment)?

Advertisement

Answer

On successful paid orders for all payment gateways others than Bank wire, cheque or Cash on delivery, you can use dedicated woocommerce_payment_complete hook located in WC_Order payment_complete() method instead of more generic hook woocommerce_thankyou, like:

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
function action_payment_complete( $order_id, $order ) {
    // Here add your code
}

Note that you can use defined $order_id and $order function arguments. Also this hook is only triggered once, avoiding repetitions.


For Bank wire (bacs), Cheque (cheque) or Cash on delivery (cod) payment methods, As the shop manager confirm manually that order is paid by changing order status, you can use the dedicated hook woocommerce_order_status_changed as follows.

add_action( 'woocommerce_order_status_changed', 'bacs_cheque_cod_payment_complete', 10, 4 );
function bacs_cheque_cod_payment_complete( $order_id, $old_status, $new_status, $order ) {
    // 1. For Bank wire and cheque payments
    if( in_array( $order->get_payment_method(), array('bacs', 'cheque') 
    && in_array( $new_status, array('processing', 'completed') 
    && ! $order->get_date_paid('edit') ) {
        // Do something
    } 

    // 2. For Cash on delivery payments
    if( 'cod' === $order->get_payment_method() && 'completed' === $new_status ) {
        // Do something
    } 
}

Note that you can use defined $order_id and $order function arguments. Also this hook will be triggered once, on order status change, avoiding repetitions.


Related: After a successful payment, What hook is triggered in Woocommerce

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement