The reference you picked is ok
https://woocommerce.com/document/automatically-complete-orders/
Just needed a small adjustment:
/**
* Auto Complete Processing WooCommerce orders on Thankyou Page
*/
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 );
if ( $order->has_status('processing') ) {
$order->update_status( 'completed' );
}
}
What we are doing with this adjustment is checking if the order has status processing before updating it to completed. This will avoid failed orders to be turned into completed.
Hope it works