创建订单从处理到待处理时设置 WooCommerce 订单状态

2021-12-22 00:00:00 php wordpress woocommerce checkout orders

创建 woocommerce 订单后,订单状态为正在处理".我需要将默认订单状态更改为待处理".

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

我怎样才能做到这一点?

How can I achieve this?

推荐答案

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

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:自动完成支付订单(取决于付款方式)

相关文章