简体   繁体   中英

Woocommerce - woocommerce_order_status_pending hook not calling


I am having trouble with checking order status of woocommerce order.

I have a plugin that I am creating and I need to know, when the order become "pending" and then "completed". But all hooks are working only if I set the order status manually in wordpress admin.

function order_status_changed_clbk( $order_id ){
    ...some code...
}
add_action( 'woocommerce_order_status_pending', 'order_status_changed_clbk' );

UPDATE

I've found out that there is a little problem. If the user cancels the payment for example at PayPal, he maybe get's redirected to the checkout again. Now let's expect that he repeats the checkout again. In this case the hook get's called a second time which could be problematic. So I've implemented myself a payment_counter :

add_action( 'woocommerce_checkout_order_processed', 'order_status_changed_clbk' ); function order_status_changed_clbk( $order_id ) { $payment_counter = (int) get_post_meta( $order_id, 'payment_counter', true );

if ( empty( $payment_counter ) ) {
    update_post_meta( $order_id, 'payment_counter', 1 );
    error_log( 'Function works!' ); //Get's called only once
} else {
    update_post_meta( $order_id, 'payment_counter', ++ $payment_counter ); //Cool thing for statistics maybe, but not really needed
}

}


Maybe this hook works for you:

function order_status_changed_clbk( $order_id ){
    error_log( 'Function works!' );
}
add_action( 'woocommerce_checkout_order_processed', 'order_status_changed_clbk' );

I'm using it within my plugin. If the order is processed, it's also "pending" so maybe this is the solution your'e looking for.

Try it out and check your debug.log for Function works! .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM