Hey there, fellow WordPress wrangler! đź‘‹
WooCommerce provides basic order statuses (like “Pending” or “Completed”), but they may not be sufficient for businesses with unique workflows. In this guide, we’ll cover how to create custom WooCommerce order statuses, add custom actions and emails, and set up automated status transitions to optimize order management.
Why Create Custom WooCommerce Order Statuses?
Imagine you run a handmade goods store. You may want to track each order with specific statuses like “In Production” and “Quality Check.” These custom statuses can give you and your customers a clearer view of each order’s progress.
Step 1: Creating Custom WooCommerce Order Statuses
First, let’s add custom order statuses. This code will add “In Production” and “Quality Check” statuses.
Code to Register Custom Order Statuses
// Register Custom Order Statuses
function ns_register_custom_order_statuses() {
register_post_status('wc-in-production', array(
'label' => 'In Production',
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop('In Production <span class="count">(%s)</span>', 'In Production <span class="count">(%s)</span>'),
));
register_post_status('wc-quality-check', array(
'label' => 'Quality Check',
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop('Quality Check <span class="count">(%s)</span>', 'Quality Check <span class="count">(%s)</span>'),
));
}
add_action('init', 'ns_register_custom_order_statuses');
Code Explanation
- register_post_status: This function creates new statuses within WooCommerce, prefixed with “wc-” to ensure WooCommerce recognizes them.
- Arguments:
- label: The name of the status (e.g., “In Production”).
- show_in_admin_status_list and show_in_admin_all_list: Allows the status to appear in the admin order status list.
- label_count: Defines how the count for each status appears in the admin list.
Adding Custom Statuses to WooCommerce
Once the statuses are registered, let’s add them to WooCommerce’s list of statuses so they’re available in the order dropdown.
function ns_add_custom_order_statuses($order_statuses) {
$order_statuses['wc-in-production'] = 'In Production';
$order_statuses['wc-quality-check'] = 'Quality Check';
return $order_statuses;
}
add_filter('wc_order_statuses', 'ns_add_custom_order_statuses');
Code Explanation
- Filter wc_order_statuses: This filter allows us to include our custom statuses in WooCommerce’s list of available order statuses.
- Returning the updated array: We return the modified $order_statuses array with our new statuses.
Step 2: Adding Custom Order Actions and Emails
To make the custom statuses more functional, we’ll add custom actions (e.g., buttons) and automated emails when orders transition to these statuses.
Adding Custom Order Actions
We’ll add an action button that lets admins set an order to “In Production” from the “Processing” status.
function ns_custom_order_actions($actions, $order) {
if ($order->get_status() == 'processing') {
$actions['ns_mark_in_production'] = array(
'url' => wp_nonce_url(admin_url('admin-ajax.php?action=ns_mark_in_production&order_id=' . $order->get_id()), 'woocommerce-mark-in-production'),
'name' => __('Mark as In Production', 'woocommerce'),
);
}
return $actions;
}
add_filter('woocommerce_order_actions', 'ns_custom_order_actions', 10, 2);
Code Explanation
- Filter woocommerce_order_actions: This filter lets us add a new action button to WooCommerce’s order actions menu.
- Checking Order Status: We only add the “Mark as In Production” action when an order is in the “Processing” status.
- Creating the Action:
- url: The URL for the action, with a nonce for security.
- name: The button label that appears in the actions menu.
Next, we’ll define the action that updates the order’s status.
function ns_process_order_action() {
if (!isset($_GET['order_id'])) return;
$order_id = intval($_GET['order_id']);
$order = wc_get_order($order_id);
$order->update_status('in-production');
wp_redirect(wp_get_referer());
exit;
}
add_action('wp_ajax_ns_mark_in_production', 'ns_process_order_action');
Code Explanation
- Action wp_ajax_ns_mark_in_production: This action triggers when our button is clicked.
- Updating Order Status: We retrieve the order and update its status to “In Production,” then redirect to the referring page.
Adding Custom Order Emails
Custom emails keep customers informed when an order reaches a new status.
1. Register Custom Emails:
function ns_register_custom_emails($email_classes) {
require_once 'path/to/class-wc-email-in-production.php';
$email_classes['WC_Email_In_Production'] = new WC_Email_In_Production();
return $email_classes;
}
add_filter('woocommerce_email_classes', 'ns_register_custom_emails');
Code Explanation
- Action woocommerce_email_classes: Allows us to add custom email classes to WooCommerce’s email handler.
- Loading the Custom Email Class: We load the custom email class file and register it.
2. Define the Custom Email Class:
In class-wc-email-in-production.php
, set up the email with subject, heading, and template.
Step 2: Adding Custom Order Actions and Emails
Automated transitions can save time by moving orders to the next status without manual updates.
Let’s say you want an order to automatically move from “In Production” to “Quality Check” after 48 hours.
function ns_auto_transition_order_status() {
$args = array(
'status' => 'wc-in-production',
'date_modified' => '>' . (time() - 48 * HOUR_IN_SECONDS),
);
$orders = wc_get_orders($args);
foreach ($orders as $order) {
$order->update_status('quality-check');
}
}
add_action('woocommerce_hourly_cron_job', 'ns_auto_transition_order_status');
Code Explanation
- Defining Order Criteria: We fetch orders with “In Production” status modified more than 48 hours ago.
- Updating Status: For each order meeting these criteria, we update the status to “Quality Check.”
- Scheduled with a Cron Job: This function should be scheduled with WooCommerce’s hourly cron job to check regularly.
Note: Set up a custom cron job if WooCommerce’s default isn’t sufficient.
Final Thoughts
Adding custom WooCommerce order statuses, actions, emails, and automated workflows can transform your order management. By following these steps and adapting the code snippets, you’ll be able to create a tailored workflow to suit your store’s unique requirements. Happy coding!