Recently, while developing a couple of custom solutions for FluentCart, I needed to replicate some of my old Woo snippets that do things, and one of those was offline order auto-complete for quick setup and testing. The snippet below is what I settled on for now.
When a COD order is placed, this will complete it and fire applicable hooks.
<?php
/**
* TEST ONLY: Auto-complete FluentCart Cash / offline_payment orders.
*/
add_action( 'fluent_cart/order_status_changed_to_on-hold', 'fc_test_autocomplete_cash_orders', 20, 1 );
add_action( 'fluent_cart/order_created', 'fc_test_autocomplete_cash_orders', 20, 1 );
add_action( 'fluent_cart/order_placed_offline', 'fc_test_autocomplete_cash_orders', 20, 1 );
function fc_test_autocomplete_cash_orders( $data ): void {
if ( ! class_exists( '\FluentCart\App\Models\Order' ) ) {
error_log( 'FC TEST: Order model not found.' );
return;
}
$incoming_order = $data['order'] ?? $data ?? null;
if ( ! $incoming_order || empty( $incoming_order->id ) ) {
error_log( 'FC TEST: No order found in hook data.' );
return;
}
$order_id = absint( $incoming_order->id );
$order = \FluentCart\App\Models\Order::find( $order_id );
if ( ! $order ) {
error_log( 'FC TEST: Order not found: ' . $order_id );
return;
}
$payment_method = strtolower( (string) ( $order->payment_method ?? '' ) );
$payment_title = strtolower( (string) ( $order->payment_method_title ?? '' ) );
$is_cash_order = (
'offline_payment' === $payment_method ||
'cash' === $payment_title ||
false !== strpos( $payment_title, 'cash' )
);
if ( ! $is_cash_order ) {
error_log( 'FC TEST: Not cash/offline payment. Order #' . $order_id . ' method=' . $payment_method . ' title=' . $payment_title );
return;
}
error_log( 'FC TEST: Auto-completing cash order #' . $order_id );
global $wpdb;
$orders_table = $wpdb->prefix . 'fct_orders';
$transactions_table = $wpdb->prefix . 'fct_order_transactions';
$wpdb->update(
$orders_table,
[
'status' => 'completed',
'payment_status' => 'paid',
'total_paid' => $order->total_amount ?? 0,
'completed_at' => current_time( 'mysql' ),
'updated_at' => current_time( 'mysql' ),
],
[ 'id' => $order_id ]
);
$wpdb->update(
$transactions_table,
[
'status' => 'paid',
'updated_at' => current_time( 'mysql' ),
],
[ 'order_id' => $order_id ]
);
$order = \FluentCart\App\Models\Order::find( $order_id );
do_action( 'fluent_cart/payment_status_changed_to_paid', [
'order' => $order,
] );
do_action( 'fluent_cart/order_status_changed_to_completed', [
'order' => $order,
] );
do_action( 'fluent_cart/order_paid_done', [
'order' => $order,
'transaction' => null,
'customer' => null,
] );
error_log( 'FC TEST: Cash order marked paid/completed and hooks fired #' . $order_id );
}PHP