Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce trigger a function on order status complete

I am trying to trigger a function when an order is completed. I am using this code:

add_action('woocommerce_order_status_completed', array($this, 
  'payment_complete' 
), 1);

and then this function:

public function payment_complete($order_id) { 
  $this->generate_order_file($order_id); 
}

This function is meant to generate an order file, but it's not being called at all. I previously tried getting it to add a message to the error log,

I am using the WooCommerce Order Status Control plugin, and orders are automatically changing to complete upon payment. Yet for some reason the woocommerce_order_status_completed action isn't triggering.

like image 968
alexkwatson Avatar asked Feb 14 '26 07:02

alexkwatson


2 Answers

The best way is using this action:

add_action( 'woocommerce_order_status_completed', 'your_function', 10, 1);
function your_function($order_id) {
}
like image 101
Vahid Avatar answered Feb 16 '26 20:02

Vahid


FUNCTION

function payment_complete( $order_id, $old_status, $new_status ){
        if( $new_status == "completed" ) {
            $this->generate_order_file($order_id); 
        }
}

HOOK

add_action( 'woocommerce_order_status_changed', array( $this, 'payment_complete'), 99, 3 );

NOTE

Make sure that the hook is executed. Is yes, this will work.

Good luck.

like image 42
hemnath mouli Avatar answered Feb 16 '26 19:02

hemnath mouli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!