Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

php - WooCommerce change order status BACS processing

In WooCommerce any order placed with the BACS (direct bank transfer) is set to "on-hold".

How would one go about changing this automatically to processing?

I wan't it to work inside the functions.php

I have the following code but that doesn't work:

add_filter( 'woocommerce_payment_complete_order_status', 'rfvc_update_order_status', 10, 2 );

function rfvc_update_order_status( $order_status, $order_id ) {
    $order = new WC_Order( $order_id );
    if ( 'on-hold' == $order_status && 'on-hold' == $order->status ) {
        return 'processing';
    }
    return $order_status;
}

Any help would be great!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

New 2020 update

WooCommerce version 3.4 has introduced a much better hook than woocommerce_thankyou or woocommerce_thankyou_bacs, that allows to change the default order status for BACS payment method.

Using this hook will:

  • clearly lighten the necessary code,
  • avoid "on-hold" notification to the customer when a BACS order is placed.

So use instead the following:

add_filter( 'woocommerce_bacs_process_payment_order_status','filter_bacs_process_payment_order_status_callback', 10, 2 );
function filter_bacs_process_payment_order_status_callback( $status, $order ) {
    return 'processing';
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


Original answer:

Update (added a version for woocommerce 3+ at the end)

It seems that woocommerce_payment_complete_order_status action hook doesn't trigger with BACS payment method.

Based on this thread, 'woocommerce_thankyou' action hook does the job:

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

    // Get an instance of the WC_Order object
    $order = new WC_Order( $order_id );
 
    if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ('on-hold' == $order->status || 'pending' == $order->status) ) {
        $order->update_status('processing');
    } else {
        return;
    }
}

Code goes in function.php file of your active child theme (or active theme). tested and works.


For woocommerce 3+ versions:

Here we use the similar composite hook woocommerce_thankyou_{$order->get_payment_method()}:

add_action( 'woocommerce_thankyou_bacs', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
 
    if ( in_array( $order->get_status(), array('on-hold', 'pending') ) ) {
        $order->update_status('processing');
    } else {
        return;
    }
}

Code goes in function.php file of your active child theme (or active theme). tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...