Home > Backend Development > PHP Tutorial > How to Automatically Complete Paid WooCommerce Orders Based on Payment Method?

How to Automatically Complete Paid WooCommerce Orders Based on Payment Method?

Mary-Kate Olsen
Release: 2024-12-11 09:18:18
Original
617 people have browsed it

How to Automatically Complete Paid WooCommerce Orders Based on Payment Method?

How to Implement Conditional Autocompletion for Paid WooCommerce Orders

In WooCommerce, virtual products often encounter issues with automatic order completion. This comprehensive guide provides multiple solutions to address this problem, including a custom code snippet and plugin options. For a more granular approach, consider implementing conditional code based on WooCommerce payment methods.

Conditional Code for Autocompleting Paid Orders

To selectively apply the autocompletion feature based on payment methods, utilize the woocommerce_payment_complete_order_status filter hook, which is triggered when a payment is required in checkout. Here's an improved version compatible with WooCommerce 3 and above:

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

This code changes the allowed paid order status to "completed" for all payment gateways except "Bank wire" (bacs), "Cash on delivery" (cod), and "Cheque" (cheque).

Additional Considerations

  • This method avoids sending multiple customer notifications for order status changes (processing to completed).
  • It's lightweight and effective, as it's only triggered when an online payment is required.

Alternatives

Improved version (2018)

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
    // No updates for Bank wire, Cash on delivery, and Cheque
    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
        return;
    } 
    // Autocomplete all others
    else {
        $order->update_status( 'completed' );
    }
}
Copy after login

Original answer (all WooCommerce versions)

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    // No updates for Bank wire, Cash on delivery, and Cheque
    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;
    } 
    // Autocomplete all others
    else {
        $order->update_status( 'completed' );
    }
}
Copy after login

Note: Be sure to place the code snippets in the functions.php file of your child theme.

The above is the detailed content of How to Automatically Complete Paid WooCommerce Orders Based on Payment Method?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template