0

I want to execute a function upon successful payment in Woocommerce.

For example, when a user makes a purchase and clicks the payment button, it would return successful.

I then want to call a function.

How would I accomplish this? Is there a filter I can use in functions.php?

Any help would greatly be appreciated.

For additional context, my function would contain an API call so if a user successfully makes a payment, it would call an API.

Khoa
  • 261
  • 1
  • 2
  • 19

1 Answers1

2

You can use "woocommerce_payment_complete" hook, which is fired when the payment is complete.

add_action( 'woocommerce_payment_complete', 'do_something_after_payment_complete' );
function do_something_after_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );
    if ( $order->has_status( 'completed' ) ) {
        // Do something
    }
}
Sumit Wadhwa
  • 2,825
  • 1
  • 20
  • 34
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • 1
    always good to check the status of the order (($order->get_status() != 'refunded') && ($order->get_status() != 'failed')) – Shir Gans Oct 19 '20 at 06:12