The “Customer invoice / Order details” WooCommerce email notification is often used for manually sending payment requests to customers – it’s not automatically triggered like other WooCommerce emails. You can access it from the individual order screen under the “Order Actions” section.
In certain cases, however, you’d want to make it automatic. And I don’t know why it’s not like that by default. So, let’s fix it: if you create a manual order from the backend, set it as pending, add some products to it and “Save”, the email notification will trigger on its own thanks to the snippet below. Enjoy!
PHP Snippet: Trigger “Customer invoice / Order details” WooCommerce Email Automatically
This WooCommerce PHP code snippet sends an automated email notification to the customer when their order status changes to “pending” and requires payment. Let’s break it down:
add_action( ‘woocommerce_order_status_pending’, ‘bbloomer_send_pending_order_email’, 9999, 2 );
- This line uses
add_action
to hook thebbloomer_send_pending_order_email
function to thewoocommerce_order_status_pending
action. This means the function will be executed whenever an order’s status changes to “pending”. - The
2
arguments indicate that the function accepts two arguments:$order_id
and$order
.
function bbloomer_send_pending_order_email( $order_id, $order ) {
- This function receives the
$order_id
and the$order
object as arguments.
if ( ! $order->needs_payment() ) return;
- This checks if the order requires payment using the
$order->needs_payment()
method. If it doesn’t (e.g., free order, order already paid), the function exits.
WC()->payment_gateways();
WC()->shipping();
WC()->mailer()->customer_invoice( $order );
- These three lines seem involve initializing WooCommerce components like payment gateways, shipping methods, and the mailer class, including the actual sending of the “customer_invoice” notification.
$order->add_order_note( __( ‘Payment request automatically sent to customer.’, ‘bbloomer’ ), false, true );
- This line adds a note to the order history stating that a payment request was automatically sent to the customer.
- The
__( 'Payment request automatically sent to customer.', 'bbloomer' )
part uses the WordPress translation function to ensure the note is displayed in the correct language based on the store’s settings. - The
false
argument prevents the note from being sent to the customer as an email notification. - The
true
argument marks the note as private, meaning it’s only visible to administrators.
/**
* @snippet Automatically Send Woo Customer Invoice Email
* @tutorial Get CustomizeWoo.com FREE
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community Join https://businessbloomer.com/club/
*/
add_action( 'woocommerce_order_status_pending', 'bbloomer_send_pending_order_email', 9999, 2 );
function bbloomer_send_pending_order_email( $order_id, $order ) {
if ( ! $order->needs_payment() ) return;
WC()->payment_gateways();
WC()->shipping();
WC()->mailer()->customer_invoice( $order );
$order->add_order_note( __( 'Payment request automatically sent to customer.', 'bbloomer' ), false, true );
}
This may have changed but currently ‘woocommerce_order_status_pending’ is not called when an order is created at checkout as “pending’ is the default status.
That could be, my aim was to target manual orders to be honest. When does the order go/stay into pending on the normal checkout?