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

Set WooCommerce order status when order is created from processing to pending(创建订单从处理到待处理时设置 WooCommerce 订单状态)
本文介绍了创建订单从处理到待处理时设置 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".

我怎样才能做到这一点?

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)