1
votes

I WooCommerce, I'm using "Change admin payment status back to unpaid for pending order status in Woocommerce" answer code to reset the paid status of orders when the order status is manually changed in the backend to pending.

So for example, it removes the following if the order status was change from "completed" to "pending": "Paid on April 2, 2019 @ 5:29 pm"

Now my problem here is after the order status was set to "pending", I tried to set it again the status to "completed" but it failed to set the date paid or completed date.

I'm using the latest version of Woocommerce Version 5.1.1

Any idea how to fix this?

1

1 Answers

2
votes

Update #1 - To solve this problem try the following:

add_action( 'woocommerce_order_status_changed', 'pending_reset_order_paid_date', 20, 4 );
function reset_order_paid_date( $order_id, $old_status, $new_status, $order ) {
    // Null paid date
    if ( in_array( $old_status, array('on-hold', 'processing', 'completed') ) && 'pending' === $new_status ) {
        $order->set_date_paid(null);
        $order->update_meta_data( '_reseted_paid_date', true ); // Add a custom meta data flag
        $order->save();
    }
    // Set paid date back when the paid date has been nulled on 'processing' and 'completed' status change
    if( $order->get_meta('_reseted_paid_date' ) && in_array( $new_status, array('pending', 'on-hold') )
        && in_array( $new_status, array('processing', 'completed') ) )
    {
        $order->set_date_paid( current_time( 'timestamp', true ) );
        $order->delete_meta_data( '_reseted_paid_date' ); // Remove the custom meta data flag
        $order->save();
    }
}

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