A useful code snippet for implementing WooCommerce order tracking solutions is to add a custom order status that allows customers to track their orders. This can be achieved by using the register_post_status()
function provided by WordPress.
function wpsnippets_add_custom_order_status() {
register_post_status( 'wc-custom-order-status', array(
'label' => _x( 'Custom Order Status', 'Order status', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Custom Order Status <span class="count">(%s)</span>', 'Custom Order Status <span class="count">(%s)</span>', 'woocommerce' )
) );
}
add_action( 'init', 'wpsnippets_add_custom_order_status' );
This code snippet registers a new custom order status called “Custom Order Status” in WooCommerce. It makes the status visible to customers and administrators, and includes it in the order status dropdowns in the admin area. You can customize the label and other parameters as needed.
Once you have added this code to your theme’s functions.php
file or a custom plugin, you will be able to assign this custom order status to orders in the WooCommerce admin area. Customers will also see this status when tracking their orders.
This code snippet can be useful when you want to add a specific order status to track orders that require special handling or have a unique status in your WooCommerce store.
Examples
Example 1: Adding Custom Order Status in WooCommerce
This use case demonstrates how to add a custom order status in WooCommerce using the register_post_status()
function.
function wpsnippets_add_custom_order_status() {
register_post_status( 'wc-custom-status', array(
'label' => _x( 'Custom Status', 'Order status', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>', 'woocommerce' )
) );
}
add_action( 'init', 'wpsnippets_add_custom_order_status' );
Explanation: This code adds a custom order status called “Custom Status” to WooCommerce. The register_post_status()
function is used to register the new status, and various parameters are passed to define its behavior and appearance in the WooCommerce admin area.
Example 2: Changing Order Status Label in WooCommerce
This use case demonstrates how to change the label of an existing order status in WooCommerce using the woocommerce_order_statuses
filter.
function wpsnippets_change_order_status_label( $statuses ) {
$statuses['wc-processing'] = _x( 'In Progress', 'Order status', 'woocommerce' );
return $statuses;
}
add_filter( 'woocommerce_order_statuses', 'wpsnippets_change_order_status_label' );
Explanation: This code modifies the label of the “Processing” order status in WooCommerce to “In Progress”. The woocommerce_order_statuses
filter is used to modify the array of order statuses, and the desired label is set using the _x()
function.
Example 3: Sending Custom Order Status Email Notifications
This use case demonstrates how to send custom email notifications for a specific order status in WooCommerce using the woocommerce_email_classes
filter.
function wpsnippets_add_custom_order_status_email( $email_classes ) {
require_once 'includes/class-wc-email-custom-status.php';
$email_classes['WC_Email_Custom_Status'] = new WC_Email_Custom_Status();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'wpsnippets_add_custom_order_status_email' );
Explanation: This code adds a custom email notification for a specific order status in WooCommerce. The woocommerce_email_classes
filter is used to add a new email class, which is defined in the class-wc-email-custom-status.php
file. The custom email class extends the WC_Email
class and handles the email content and sending logic.