繁体   English   中英

设置WooCommerce 订单创建时订单状态从processing到pending

[英]Set WooCommerce order status when order is created from processing to pending

创建 woocommerce 订单时,订单状态为“处理中”。 我需要将默认订单状态更改为“待处理”。

我怎样才能做到这一点?

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

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

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

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

对于货到付款付款方式,可以使用专用过滤器挂钩进行调整,请参阅:
在 Woocommerce 中将货到付款默认订单状态更改为“暂停”而不是“处理中”

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

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语句中的条件将不再匹配,并且不会发生任何其他事情。


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

如今,如果您使用的支付网关使用WC_Order->payment_complete()正确设置订单状态,您可以使用woocommerce_payment_complete_order_status过滤器。

这比使用woocommerce_thankyou hook 更好,因为我们立即设置订单状态,而不是在设置后应用它。

function h9dx3_override_order_status($status, $order_id, $order) {
  if ($status === 'processing') {
    $status = 'pending';
  }

  return $status;
}
add_filter('woocommerce_payment_complete_order_status', 'h9dx3_override_order_status', 10, 3);

同样,这仅在支付网关使用正确的payment_complete包装器方法而不是直接使用set_status设置状态时才有效。 您可以在网关代码中搜索“payment_complete(”和“set_status(”,看看它做了什么。

如果你为每个人开发一个插件,你最好使用woocommerce_thankyou ,或者你可以使用组合方法并在订单状态未更新时使用woocommerce_thankyou作为后备。

此条件可防止客户端在初始更改 3 分钟后刷新页面时不自觉地更改状态。

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

    $time_order     = strtotime($order->get_date_created());
    $time_current   = time();
    $time_interval  = $time_current - $time_order;

    //Case refresh page after 3 minutes at order, no changed status
    if( $order->get_status() == 'processing' && $time_interval < 180 ) {
        $order->update_status( 'pending' );
    }
}

挂钩 woocommerce_thankyou 遇到的问题是您可以支付订单然后关闭浏览器或 go 其他地方,因此永远不要点击“返回商户”(通过 paypal 支付后显示)进入谢谢页面。 然后代码将永远不会被执行。

// Rename order status 'Processing' to 'Order Completed' in admin main view - different hook, different value than the other places
add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );
function wc_renaming_order_status( $order_statuses ) {
    foreach ( $order_statuses as $key => $status ) {
        if ( 'wc-processing' === $key ) 
            $order_statuses['wc-processing'] = _x( 'Order Completed', 'Order status', 'woocommerce' );
    }
    return $order_statuses;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM