简体   繁体   中英

Changing Woocommerce Order Status based on order notes

I've been looking around to find a way to change our woocommerce orders status based on the order notes. We have a card processor that adds order notes when an order is approved, but it puts the order on hold because the card is only authorized not charged. We charge the card later once the order has been shipped, but we need the order to go into "processing" so we can export the order into our system so we can actually process the order (we don't process through woocommerce).

I found this code however, I'm wondering if this can be modified to pull what is in the order notes since our processor adds whether or not the card has been approved.

function mysite_woocommerce_payment_complete( $order_id ) {
    error_log( "Payment has been received for order $order_id" );
}
add_action( 'woocommerce_payment_complete', 'mysite_woocommerce_payment_complete', 10, 1 );

Here is a Example which switch on hold order status with processing. you can use it or if need to add some more condition, please mention here or update it accordingly. Thanks

function switch_hold_status_to_processing ($order_id) {
  $order = new WC_Order( $order_id );
  $order->update_status('processing');
}
add_action('woocommerce_order_status_on-hold', 'switch_hold_status_to_processing');

You can use wc_get_order_notes($args) function to get the order notes in the specific order. It returns an array of order notes. Then you can loop through the array to find the order note you need. Then use an if statement to verify the content of the order note and update the status.

function mysite_woocommerce_payment_complete( $order_id ) {
    $order_notes = wc_get_order_notes([
        'order_id' => $order_id,
     ]);
    foreach ($order_notes as $order_note){
        if ($order_note->content == "Order verified"){
            $order = new WC_Order( $order_id );
            $order->update_status('processing');
            break;
        }
    }
    
}
add_action( 'woocommerce_payment_complete', 'mysite_woocommerce_payment_complete', 10, 1 );

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