Reputation: 35
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.
Upvotes: 2
Views: 14635
Reputation: 43
Try the below code, change with simple if condition
add_action('woocommerce_order_status_completed', 'so_status_completed', 10, 3);
function so_status_completed($order_id)
{
if( get_post_meta( $order_id, 'download_link', true ) ){
$note = get_post_meta( $order_id, 'download_link', true );
} else {
$note = __("Status updated with no link.");
}
// The order note or you can change as your requirement
if( isset($note) && ! empty($note) ){
$order = wc_get_order( $order_id ); // The WC_Order Object
$order->add_order_note( $note ); // Add the note
$order->save(); // Save the order
}
}
Upvotes: 0
Reputation: 7561
The best way is using this action:
add_action( 'woocommerce_order_status_completed', 'your_function', 10, 1);
function your_function($order_id) {
}
Upvotes: 6
Reputation: 2755
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.
Upvotes: 3