在WooCommerce中更改付款订单状态更改的已付款日期 [英] Set back date paid on paid order statuses change in WooCommerce

查看:179
本文介绍了在WooCommerce中更改付款订单状态更改的已付款日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,如果订单状态从已完成"更改为待处理",它将删除以下内容:"2019年4月2日下午5:29支付"

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.

我正在使用最新版本的Woocommerce版本5.1.1

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

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

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

这篇关于在WooCommerce中更改付款订单状态更改的已付款日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆