In WooCommerce, several hooks are triggered after a user completes a payment. We can attach custom processing functions to these hooks to implement specific business logic for orders.
There are three main types of WooCommerce hooks triggered after payment completion.
woocommerce_pre_payment_completewoocommerce_payment_completewoocommerce_payment_complete_order_status_$status
All of these hooks are triggered when an order has been paid or doesn’t require payment (e.g., Cash on Delivery). They also apply to custom payment gateways.
woocommerce_pre_payment_complete
The first hook (woocommerce_pre_payment_complete) is used when the payment process begins and does not depend on the order status.
add_action( 'woocommerce_pre_payment_complete', 'wprs_pre_complete' );
function wprs_pre_complete( $order_id ) {
$order = wc_get_order( $order_id );
// Get order data for further processing
}
- All code snippets in this article can be inserted into the
functions.phpfile of your current theme or an appropriate file within a plugin. - A wealth of information can be retrieved from the
$orderobject, such as$order->get_items()for line items,$order->get_payment_method()for the payment method title, or$order->get_user_id()for the customer ID.
woocommerce_payment_complete
The woocommerce_payment_complete hook is triggered only when the order is in one of the following statuses: on-hold, pending, failed, or cancelled. Note that this status list can be filtered via woocommerce_valid_order_statuses_for_payment_complete.
Before the hook is applied, WooCommerce changes the order status to processing or completed. This target status can also be filtered using woocommerce_payment_complete_order_status.
add_action( 'woocommerce_payment_complete', 'wprs_complete' );
function wprs_complete( $order_id ) {
$order = wc_get_order( $order_id );
// Perform subsequent actions
}
woocommerce_payment_complete_order_status_$status
Finally, woocommerce_payment_complete_order_status_$status triggers for the remaining order 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 subsequent actions
}
One important thing to note is that payment completion doesn’t always guarantee a successful payment transaction. In your hook handler, you should further verify the order status. For added security, you might even verify the payment amount through the payment gateway API to confirm that the user has successfully paid before proceeding with success-related logic.
