Send WooCommerce Customer Invoices with a Single Click
Managing customer orders in WooCommerce can be a time-consuming task, especially when it comes to generating invoices. Manually creating and sending invoices for every order can eat into your valuable time. Thankfully, there's a solution that streamlines this process: adding a custom "Send Invoice" button to the WooCommerce admin orders list. This allows you to instantly generate and email customer invoices with a single click, saving you time and effort.
The Problem: Manually Generating and Sending Invoices
Here's a typical scenario:
- You receive an order in your WooCommerce store.
- You need to generate an invoice for the customer.
- You open the order details, download the invoice as a PDF, and then manually email it to the customer.
This process can be repeated for every single order, wasting valuable time and potentially leading to errors.
The Solution: A "Send Invoice" Button
Instead of manual labor, we can automate this task with a custom "Send Invoice" button. Let's see how this works in action:
Original Code:
add_action( 'woocommerce_admin_order_actions_end', 'send_invoice_button', 10, 1 );
function send_invoice_button( $order ) {
$order_id = $order->get_id();
$invoice_url = admin_url( 'admin-ajax.php' ) . '?action=generate_invoice&order_id=' . $order_id;
echo '<a href="' . esc_url( $invoice_url ) . '" class="button" style="background-color: #4CAF50; color: white;">Send Invoice</a>';
}
add_action( 'wp_ajax_generate_invoice', 'generate_invoice' );
add_action( 'wp_ajax_nopriv_generate_invoice', 'generate_invoice' );
function generate_invoice() {
$order_id = isset( $_GET['order_id'] ) ? sanitize_text_field( $_GET['order_id'] ) : false;
if ( $order_id ) {
$order = wc_get_order( $order_id );
$invoice_pdf = WC()->mailer()->get_invoice( $order );
$invoice_html = $invoice_pdf->get_html();
// Send the invoice email
$email_headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>',
);
wp_mail( $order->get_billing_email(), 'Your Invoice', $invoice_html, $email_headers );
wp_send_json_success( array( 'message' => 'Invoice sent successfully!' ) );
} else {
wp_send_json_error( array( 'message' => 'Invalid order ID.' ) );
}
}
Explanation:
- Add the Button: This code adds a new "Send Invoice" button to the "Actions" section of each order in the WooCommerce admin orders list.
- AJAX Request: Clicking the button triggers an AJAX request to the
generate_invoice
function. - Invoice Generation: This function retrieves the order ID, generates the invoice PDF using WooCommerce's built-in functionality, and converts it to HTML for email delivery.
- Email Delivery: The invoice HTML is sent to the customer's email address via the
wp_mail()
function.
Benefits of a "Send Invoice" Button
- Save Time: Eliminate manual invoice generation and emailing, freeing up valuable time for other tasks.
- Improved Efficiency: Streamline your workflow and reduce errors associated with manual processes.
- Enhanced Customer Experience: Provide customers with timely and professional invoices, improving their satisfaction.
Adding Value: Further Enhancements
Here are some ways to make your "Send Invoice" button even more powerful:
- Custom Invoice Templates: Use custom invoice templates designed to match your branding and requirements.
- Automatic Invoice Numbering: Implement a system that automatically assigns unique invoice numbers for each order.
- Custom Invoice Content: Add specific details like payment terms, company information, or additional notes to your invoices.
- Integration with Payment Gateways: Sync invoice generation with payment statuses, ensuring invoices are sent only after successful payment.
Conclusion
Adding a "Send Invoice" button to your WooCommerce admin orders list is a simple yet effective solution to streamline your invoicing process. By automating this task, you can significantly improve efficiency, save time, and enhance your customer experience.
Remember: This code is a starting point. Adjust it to fit your specific needs and implement the enhancements mentioned above to create a fully customized and powerful invoicing solution for your WooCommerce store.