Change admin payment status back to unpaid for pen

2019-08-13 23:09发布

问题:

When Order status in Woocommerce is changed to Processing the payment status is set to paid:

But order was accidentally set to processing and shouldn't have gotten the status paid. Now when we set status to pending again it doesn't remove the text:

Order #1234 details
Payment via Purchase Order. Paid on September 17, 2018 @ 9:18 am

Any idea how to change this text to what is was before status was changed?

回答1:

Use the following code that will reset (empty) paid date, so it will remove the paid message.

So each time that an order that have a status as "processing", "completed" or "On Hold" is passed back to "Pending" status, the paid date will be emtied.

The code:

add_action( 'woocommerce_order_status_changed', 'reset_order_paid_date', 20, 4 );
function reset_order_paid_date( $order_id, $old_status, $new_status, $order ){
    if ( in_array( $old_status, array('on-hold', 'processing', 'completed') ) && $new_status == 'pending' ) {
        $order->set_date_paid(null);
        $order->save();
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

To make it effective for your problematic order, run the following code only once, pasting it on function.php child theme's file. Then browse any page of your web site and remove it…
(where 123 is the order ID that you have to replace by your order ID)

$order = wc_get_order( 123 ); // <== HERE set your order number
$order->set_date_paid(null);
$order->save();

Related: Set back date paid on paid order statuses change in WooCommerce