June 29, 2019

Custom Order Handling After Payment Completion in WooCommerce

In WooCommerce, when a customer completes their payment, several hooks are triggered.
We can attach our custom functions to these hooks to perform any additional processing we need after payment is completed.

WooCommerce provides three main hooks for this purpose:

  • woocommerce_pre_payment_complete
  • woocommerce_payment_complete
  • woocommerce_payment_complete_order_status_$status

All these hooks are triggered when an order has been paid for, or when payment is not required (for example, for “Cash on Delivery” orders).
They also work with custom payment gateways.

woocommerce_pre_payment_complete

The first hook, woocommerce_pre_payment_complete, is fired at the start of the payment completion process, regardless of the order status.

Example usage:

add_action( 'woocommerce_pre_payment_complete', 'wprs_pre_complete' );

function wprs_pre_complete( $order_id ) {
    $order = wc_get_order( $order_id );

    // Fetch order data for further processing
}

You can place all the code snippets from this article into your theme’s functions.php file or into the appropriate file in your custom plugin.

With the $order object, you can retrieve a lot of useful information:

  • $order->get_items() — Get the order items
  • $order->get_payment_method() — Get the payment method title
  • $order->get_user_id() — Get the customer ID …and much more.

woocommerce_payment_complete

The woocommerce_payment_complete hook is only fired when the order status is one of the following:
on-hold, pending, failed, or cancelled.

Keep in mind:

  • This list can be filtered using woocommerce_valid_order_statuses_for_payment_complete.
  • Before this hook runs, WooCommerce will update the order status to either processing or completed.
    This behavior itself can be filtered using woocommerce_payment_complete_order_status.

Example usage:

add_action( 'woocommerce_payment_complete', 'wprs_complete' );

function wprs_complete( $order_id ) {
    $order = wc_get_order( $order_id );

    // Perform post-payment operations
}

woocommerce_payment_complete_order_status_$status

Finally, the woocommerce_payment_complete_order_status_$status hook lets you handle specific order statuses after payment is completed.

Example for processing and completed statuses:

add_action( 'woocommerce_payment_complete_order_status_processing', 'wprs_complete_for_status' );
add_action( 'woocommerce_payment_complete_order_status_completed', 'wprs_complete_for_status' );

function wprs_complete_for_status( $order_id ) {
    $order = wc_get_order( $order_id );

    // Perform actions based on specific status
}

Important Note

Payment completion does not always mean the payment was successful.
In the hooked function, it is good practice to double-check the order status.
For extra caution, you could also verify the payment amount with the payment gateway to ensure the customer has truly paid before processing the order as successful.