'Wordpress Hook for only order processing
I am making a WordPress plugin, where I need to make an API call every time order status updates to "processing" from "pending".
function __construct(){
add_action( 'woocommerce_after_order_object_save', [$this,'action_woocommerce_doba_order_import'], 10, 1 );
}
public function action_woocommerce_doba_order_import($order){
if ( 'processing' === $order->get_status() ) {
"API call here"
}
}
This code works fine when order status updates to "processing" from "pending" but it makes two additional API calls when status changes to something else from "processing". So I get two additional API calls for each order it status changes from processing to something else. I am definitely making some mistakes. Maybe I am using the wrong hook or need to put a different condition.
Solution 1:[1]
We have created a WordPress code for changing the order status, Hooks function call every time order status updates to "processing" from "pending".
You try this:
function __construct(){
add_action( 'woocommerce_after_order_object_save', [$this,'action_woocommerce_doba_order_import'], 10, 1 );
}
public function action_woocommerce_doba_order_import($order_id){
if( ! $order_id ) return;
$order = wc_get_order( $order_id );
if( $order->get_status() == 'pending' ) {
$order->update_status( 'processing' );
}
}
Solution 2:[2]
I tried to find this action in the newest version of the WooCommerce plugin [6.3.1] and I couldn't, but I found out that there's probably a better hook to use, which includes the new status.
do_action( 'woocommerce_order_edit_status', $this->get_id(), $result['to'] );
So you could use it like this:
function __construct(){
add_action( 'woocommerce_order_edit_status', [$this,'action_woocommerce_doba_order_import'], 10, 2 );
}
public function action_woocommerce_doba_order_import($order_id, $new_status){
if( ! $order_id ) return;
//ignore other status changes without additional calls
if( 'pending' !== $new_status ) return;
$order = wc_get_order( $order_id );
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dotsquares |
Solution 2 | MythThrazz |