从处理到挂起创建订单时设置WooCommerce订单状态 [英] Set WooCommerce order status when order is created from processing to pending

查看:405
本文介绍了从处理到挂起创建订单时设置WooCommerce订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建woocommerce订单时,该订单的状态为处理中".我需要将默认的订单状态更改为待处理".

When a woocommerce order is created the status of the order is "processing". I need to change the default order-status to "pending".

我该如何实现?

推荐答案

默认订单状态由付款方式或付款网关设置.

The default order status is set by the payment method or the payment gateway.

您可以尝试使用此自定义的挂钩函数,但不起作用 (因为此挂钩在付款方式和付款网关之前被触发):

You could try to use this custom hooked function, but it will not work (as this hook is fired before payment methods and payment gateways):

add_action( 'woocommerce_checkout_order_processed', 'changing_order_status_before_payment', 10, 3 );
function changing_order_status_before_payment( $order_id, $posted_data, $order ){
    $order->update_status( 'pending' );
}

显然,每种付款方式(和付款网关)都在设置订单状态(取决于付款网关的交易响应)…

Apparently each payment method (and payment gateways) are setting the order status (depending on the transaction response for payment gateways)…

对于货到付款付款方式,可以使用专用的过滤器挂钩对其进行调整,请参见:
更改货到付款默认订单状态为保留"而不是处理中"在Woocommerce中

For Cash on delivery payment method, this can be tweaked using a dedicated filter hook, see:
Change Cash on delivery default order status to "On Hold" instead of "Processing" in Woocommerce

现在,您可以使用woocommerce_thankyou 钩子更新订单状态:

Now instead you can update the order status using woocommerce_thankyou hook:

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
    if( ! $order_id ) return;

    $order = wc_get_order( $order_id );

    if( $order->get_status() == 'processing' )
        $order->update_status( 'pending' );
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

经测试可正常工作

注意:每次加载接收订单的页面时都会触发钩子woocommerce_thankyou,因此需要谨慎使用...
现在,上方的功能将仅在第一次更新订单状态.如果客户重新加载页面,则IF语句中的条件将不再匹配,并且不会发生其他任何事情.

Note: The hook woocommerce_thankyou is fired each time the order received page is loaded and need to be used with care for that reason...
Now the function above will update the order status only the first time. If customer reload the page, the condition in the IF statement will not match anymore and nothing else will happen.


相关主题: WooCommerce:自动完成已付款的订单(取决于有关付款方式)

这篇关于从处理到挂起创建订单时设置WooCommerce订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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