2

我需要通过检查进入“处理”状态而不是“暂停”状态来使 WooCommerce 推送付款。我尝试了下面的代码段,但它似乎没有效果。

这是我的代码:

add_filter( 'woocommerce_payment_complete_order_status', 'sf_wc_autocomplete_paid_orders' );

function sf_wc_autocomplete_paid_orders( $order_status, $order_id ) {

$order = wc_get_order( $order_id );

if ($order->status == 'on-hold') {
    return 'processing';
}

return $order_status;
}

我怎样才能做到这一点?

谢谢。

4

3 回答 3

12

这是您正在查看的钩子中的函数woocommerce_thankyou

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

    $order = wc_get_order( $order_id );

    // Updating order status to processing for orders delivered with Cheque payment methods.
    if (  get_post_meta($order->id, '_payment_method', true) == 'cheque' )
        $order->update_status( 'processing' );
}

此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

这是经过测试和工作的。


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

于 2016-10-07T15:48:52.220 回答
2

我不想使用Thank You过滤器,以防订单在上一步中仍设置为“暂停”,然后在过滤器中将其更改为我想要的状态(在我的情况下为自定义状态,或在您的情况下为正在处理)。所以我在检查网关中使用了过滤器:

add_filter( 'woocommerce_cheque_process_payment_order_status', 'myplugin_change_order_to_agent_processing', 10, 1 );
function myplugin_change_order_to_agent_processing($status){
    return 'agent-processing';
}

我希望这可以帮助其他人知道还有另一种选择。

于 2019-08-09T13:44:46.723 回答
0

LoicTheAztec 的先前答案已过时,并给出了有关直接在订单对象上访问对象字段的错误。

正确的代码应该是

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

    $order = wc_get_order( $order_id );

    // Updating order status to processing for orders delivered with Cheque payment methods.
    if (  get_post_meta($order->get_id(), '_payment_method', true) == 'cheque' )
        $order->update_status( 'processing' );
}
于 2020-09-14T12:26:10.947 回答