WooCommerce: Auto complete paid Orders (depending

2019-01-02 16:08发布

Normally wooCommerce should autocomplete orders for virtual products. But it doesn't and this is a real problem, even a BUG like.

So at this point you can find somme helpful things(but not really convenient):

1) A snippet code (that you can find in wooCommerce docs):

/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

But this snippet does not work for BACS*, Pay on delivery and Cheque payment methods. It's ok for Paypal and Credit Card gateways payment methods.

*BACS is a Direct Bank transfer payment method

And …

2) A plugin: WooCommerce Autocomplete Orders

This plugin works for all payment methods, but not for other Credit Card gateways payment methods.

My question:

Using (as a base) the wooCommerce snippet in point 1:

How can i implement conditional code based on woocommerce payment methods?

I mean something like: if the payment methods aren't "BACS", "Pay on delivery" and "Cheque" then apply the snippet code (update status to "completed" for paid orders concerning virtual products).

I am not a wooCommerce mega expert coder, so I haven't find yet how to target payment methods in woocommerce orders.

Some help will be very nice.

Thanks.

1条回答
路过你的时光
2楼-- · 2019-01-02 16:47

I found a solution to this problem (Works with WC 3+):

/**
 * AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
    return;

    $order = wc_get_order( $order_id );

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( ( 'bacs' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cod' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cheque' == get_post_meta($order_id, '_payment_method', true) ) ) {
        return;
    } 
    // "completed" updated status for paid Orders with all others payment methods
    else {
        $order->update_status( 'completed' );
    }
}

Code goes in function.php file of the active child theme (or active theme).

With the help of this post: How to check payment method on a WooCommerce order by id?

with this : get_post_meta( $order_id, '_payment_method', true ); from helgatheviking

Bank wire, Cash on delivery and Cheque payment methods are ignored and keep they original order status.

Updated the code for compatibility with WC 3.0+ (2017-06-10)


Enhanced version for WooCommerce 3+ (2018)

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
        return;

    // Get an instance of the WC_Product object
    $order = wc_get_order( $order_id );

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
        return;
    // Updated status to "completed" for paid Orders with all others payment methods
    } else {
        $order->update_status( 'completed' );
    }
}

Code goes in function.php file of the active child theme (or active theme).

查看更多
登录 后发表回答