在 WooCommerce 中设置支付订单状态更改的支付日期

Set back date paid on paid order statuses change in WooCommerce

我是 WooCommerce,我正在使用 "" 答案代码在后台手动将订单状态更改为待处理时重置订单的已付款状态.

因此,例如,如果订单状态从 "completed" 更改为 "pending",它会删除以下内容:"Paid on April 2, 2019 @ 5:29 pm"

现在我的问题是在订单状态设置为 "pending" 后,我尝试再次将其状态设置为 "completed" 但未能设置付款日期或完成日期。

我使用的是最新版本的 Woocommerce 版本 5.1.1

知道如何解决这个问题吗?

更新 #1 - 要解决此问题,请尝试以下操作:

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();
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。