Set back date paid on paid order statuses change in WooCommerce

主宰稳场 提交于 2020-01-05 07:16:05

问题


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:


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.



来源:https://stackoverflow.com/questions/55467742/set-back-date-paid-on-paid-order-statuses-change-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!