Home > Backend Development > PHP Tutorial > How Can I Automate WooCommerce Order Completion for Specific Payment Methods?

How Can I Automate WooCommerce Order Completion for Specific Payment Methods?

DDD
Release: 2024-12-11 06:32:09
Original
702 people have browsed it

How Can I Automate WooCommerce Order Completion for Specific Payment Methods?

Overcoming the Limitations of WooCommerce's Order Autocompletion

Normally, WooCommerce automates the completion of orders for virtual products. However, for payment methods such as "Bank wire," "Cash on delivery," and "Cheque," this functionality may not be available. To address this issue and conditionally apply order autocompletion based on payment methods, the following solutions can be implemented:

Using the woocommerce_payment_complete_order_status Filter Hook

This filter hook is triggered when a payment is required during checkout and is used by all payment methods. By modifying the allowed paid order statuses, you can achieve conditional autocompletion:

add_filter( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    return 'completed';
}
Copy after login

Utilizing the woocommerce_thankyou Action Hook

For WooCommerce versions 3 and above, the following code can be used:

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

    $order = wc_get_order( $order_id );

    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
        return;
    } else {
        $order->update_status( 'completed' );
    }
}
Copy after login

Original Solution

For all WooCommerce versions, the following code can be used:

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

    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;
    } elseif( $order->get_status()  === 'processing' ) {
        $order->update_status( 'completed' );
    }
}
Copy after login

These solutions provide conditional order autocompletion for paid orders based on the specified payment methods, ensuring efficient and accurate order processing in WooCommerce stores that accept a variety of payment options.

The above is the detailed content of How Can I Automate WooCommerce Order Completion for Specific Payment Methods?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template