Payment Reminder for Unpaid Orders
Re-sends the order details with a payment link to customers whose order is still pending payment a few hours after checkout, recovering failed or abandoned payments.
Opens WPIDE on
Code
$wpide_reminder_hours = 6; // Hours after checkout.
add_action( 'init', function () {
if ( ! wp_next_scheduled( 'wpide_unpaid_order_reminders' ) ) {
wp_schedule_event( time() + 600, 'hourly', 'wpide_unpaid_order_reminders' );
}
} );
add_action( 'wpide_unpaid_order_reminders', function () use ( $wpide_reminder_hours ) {
if ( ! function_exists( 'wc_get_orders' ) ) {
return;
}
$orders = wc_get_orders( array(
'status' => 'pending',
'date_created' => ( time() - 3 * DAY_IN_SECONDS ) . '...' . ( time() - $wpide_reminder_hours * HOUR_IN_SECONDS ),
'limit' => 20,
'meta_query' => array( array( 'key' => '_wpide_payment_reminder', 'compare' => 'NOT EXISTS' ) ), // phpcs:ignore WordPress.DB.SlowDBQuery
) );
$emails = WC()->mailer()->get_emails();
foreach ( $orders as $order ) {
if ( ! $order->get_billing_email() || ! $order->needs_payment() ) {
continue;
}
$order->update_meta_data( '_wpide_payment_reminder', time() );
$order->save_meta_data();
if ( isset( $emails['WC_Email_Customer_Invoice'] ) ) {
$emails['WC_Email_Customer_Invoice']->trigger( $order->get_id(), $order );
$order->add_order_note( 'Payment reminder sent to the customer.' );
}
}
} );
Notes
Uses WooCommerce's "Order details" email, sends one reminder per order and handles 20 orders per hourly WP-Cron run.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.