How to Send Completed Order Notifications to Admin in WooCommerce: A Beginner’s Guide
So, you’ve set up your WooCommerce store, customers are buying, and orders are flowing in! That’s fantastic! But what happens when an order is marked as “Completed?” Do you, the admin, know about it right away? By default, WooCommerce might not always send you a specific notification when an order reaches the “Completed” status. This can lead to delays in fulfilling the order or simply missing important updates.
This article will walk you through how to ensure you, the admin, get notified every time an order is marked as “Completed” in your WooCommerce store. We’ll keep it simple and straightforward, perfect for beginners!
Why is this important? Think of it like this:
Imagine you sell personalized t-shirts. A customer places an order and marks it as urgent. You’ve set up a process where once an order is “Completed” (meaning you’ve shipped it), your shipping team automatically generates a tracking number email for the customer. If you don’t know the order is completed, the tracking number email might be delayed, causing customer frustration. Getting these notifications ensures your workflow runs smoothly.
Understanding WooCommerce Order Statuses
Before we dive into the “how,” let’s quickly recap WooCommerce order statuses:
- Pending Payment: Order received but payment hasn’t been confirmed.
- Processing: Payment received and order is being prepared.
- On Hold: Awaiting payment confirmation or some action.
- Completed: Order fulfilled and complete.
- Cancelled: Order cancelled.
- Refunded: Order refunded.
- Failed: Order failed to process.
- Directly Edit the `functions.php` File: This is the most direct method, but it’s also the riskiest. You can find the file under Appearance > Theme Editor (or Customize > Theme File Editor). Be very careful when editing this file!
- Use a Child Theme: This is the recommended approach. A child theme allows you to make modifications without directly altering your main theme, ensuring that your changes won’t be overwritten during theme updates. If you don’t have a child theme, you’ll need to create one. There are many tutorials online on how to create a WordPress child theme.
- Use a Code Snippets Plugin: Plugins like “Code Snippets” allow you to add PHP code snippets to your site without directly editing theme files. This is a safer and more manageable way to add custom code.
We’re focusing on the “Completed” status because this often triggers a chain of events like sending shipping confirmations or updating inventory.
Method 1: Using WooCommerce Settings (Simple but Limited)
WooCommerce does have some built-in notification settings. Let’s see if they can help us.
1. Navigate to WooCommerce Settings: Go to your WordPress dashboard, find “WooCommerce” in the left-hand menu, and click “Settings.”
2. Click on the “Emails” Tab: You’ll see a number of tabs at the top of the page. Click on “Emails.”
3. Review the “Completed Order” Email: Find the “Completed order” email in the list. Click on “Manage” to see its settings.
4. Ensure it’s Enabled: Make sure the “Enable this email notification” checkbox is checked.
5. Check the “Recipient(s)” Field: Verify that the correct admin email address is listed in the “Recipient(s)” field. This is the email address that will receive the notification when *any* order is marked completed.
While this method is simple, it’s not ideal in many scenarios. It sends the same notification for all orders, and you may want more control. Let’s explore a more powerful method.
Method 2: Using Custom Code (The More Flexible Approach)
This method involves adding a small snippet of code to your website. Don’t worry; we’ll break it down step-by-step.
Important: Before modifying any code on your website, it’s highly recommended to create a backup of your WordPress installation. This way, if anything goes wrong, you can easily restore your site to its previous state.
1. Accessing Your Theme’s `functions.php` File:
You have a couple of options:
2. Adding the Custom Code:
Paste the following code into your `functions.php` file (or using the Code Snippets plugin):
/**
if ( ! $order ) {
return; // Exit if order not found
}
$to = ‘[email protected]’; // Replace with your admin email address
$subject = ‘WooCommerce Order Completed: #’ . $order->get_order_number();
$body = ‘A WooCommerce order has been marked as completed. Details below:’ . “nn”;
$body .= ‘Order Number: ‘ . $order->get_order_number() . “n”;
$body Explore this article on How To Add Custom Drop Down Check Box Woocommerce Registration .= ‘Customer Name: ‘ . $order->get_billing_first_name() . ‘ ‘ . $order->get_billing_last_name() . “n”;
$body .= ‘Order Total: ‘ . $order->get_formatted_order_total() . “n”;
$body .= ‘View Order: ‘ . admin_url( ‘post.php?post=’ . $order_id . ‘&action=edit’ ) . “n”;
$headers = array(‘Content-Type: text/html; charset=UTF-8’);
wp_mail( $to, $subject, $body, $headers );
}
add_action( ‘woocommerce_order_status_completed’, ‘custom_woocommerce_completed_order_notification’, 10, 1 );
3. Explanation of the Code:
- `custom_woocommerce_completed_order_notification( $order_id )`: This is the function we’re creating to send the email. It takes the order ID as an argument.
- `wc_get_order( $order_id )`: This retrieves the order object using the order ID.
- `$to = ‘[email protected]’;`: Replace `’[email protected]’` with your actual admin email address. This is where the notification will be sent.
- `$subject`: Defines the subject line of the email. It includes the order number for easy identification.
- `$body`: Constructs the email body. It includes essential order information like the order number, customer name, total amount, and a direct link to view the order in the WordPress admin panel. This link is incredibly helpful for quickly accessing the order details.
- `$headers`: Sets the email content type to HTML so you can potentially use HTML formatting in the email body if needed.
- `wp_mail( $to, $subject, $body, $headers )`: This is the WordPress function that actually sends the email.
- `add_action( ‘woocommerce_order_status_completed’, ‘custom_woocommerce_completed_order_notification’, 10, 1 )`: This line tells WordPress to run our `custom_woocommerce_completed_order_notification` function whenever an order’s status is changed to “Completed.” The `10` is the priority (lower numbers run earlier), and `1` is the number of arguments passed to the function (in this case, the order ID).
4. Save Your Changes:
- If you’re using the Theme Editor, click “Update File.”
- If you’re using a Child Theme, save the `functions.php` file.
- If you’re using a Code Snippets plugin, activate the snippet.
5. Test It Out!
The best way to confirm that your code is working correctly is to simulate a completed order:
- Log into your WooCommerce dashboard.
- Go to the Orders tab.
- Find a test order (or create one if you haven’t already).
- Edit the order and change the order status to Completed.
- Check your admin email inbox. You should receive a notification with the order details.
Customizing the Email (Taking it a Step Further)
The code provided is a solid foundation. You can customize it further to include more specific information in the email:
- Add Product Details: You can loop through the order items and include the product names and quantities.
- Include Shipping Address: Add the customer’s shipping address to the email body.
- Conditional Logic: Send different notifications based on the order total or the customer’s location.
To do this, you’ll need to dive deeper into the WooCommerce API and learn how to access order data. The WooCommerce documentation is an excellent resource for this.
Troubleshooting
- Email Not Received:
- Double-check that the email address in the `$to` variable is correct.
- Check your spam folder.
- Your server might not be configured to send emails properly. Consider using an SMTP plugin to route emails through a reliable email service.
- Website Errors:
- If you edited the `functions.php` file directly and your website is broken, revert to a previous backup or use FTP to remove the code.
- If you’re using a Code Snippets plugin, deactivate the snippet.
By following these steps, you can ensure that you’re promptly notified whenever an order is marked as “Completed” in your WooCommerce store, streamlining your workflow and improving customer satisfaction. Remember to always back up your website before making changes to code! Good luck!