Hooks Triggered When Payment Is Completed in WooCommerce

In WooCommerce, a number of hooks are triggered after a user completes payment. We can hook custom order-processing functions onto those hooks to implement the custom behavior we need.

After payment, there are three different WooCommerce hooks:

  • woocommerce_pre_payment_complete
  • woocommerce_payment_complete
  • woocommerce_payment_complete_order_status_$status

All of these hooks are triggered when an order has been paid, or when payment is not required, such as cash on delivery. They also apply to custom payment gateways.

woocommerce_pre_payment_complete

The first hook, woocommerce_pre_payment_complete, is used at the beginning of the payment-completion process 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 );

	// Retrieve order data for later processing
	
}
  • All code snippets in this article can be inserted into the current theme’s functions.php file, or into a suitable file in a plugin.
  • We can retrieve a lot of order information from the $order object, 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

woocommerce_payment_complete is triggered only when the order is in one of the following statuses: on-hold, pending, failed, or cancelled. But remember that this status list can also be filtered through woocommerce_valid_order_statuses_for_payment_complete.

Before the hook is applied, WooCommerce changes the order status to either processing or completed. That can also be filtered through woocommerce_payment_complete_order_status.

add_action( 'woocommerce_payment_complete', 'wprs_complete' );

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

	// Perform later actions
	
}

woocommerce_payment_complete_order_status_$status

Finally, woocommerce_payment_complete_order_status_$status is used to trigger handling 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 later actions
}

One important thing to note is that payment completion does not automatically mean payment success. Inside the functions attached to these hooks, we still need to check the order status further. If we want to be more cautious, we can even confirm the paid amount with the payment gateway to verify that the customer has actually paid successfully before running our payment-success logic.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *