- The first function named
add_backorder_order_status
is defining a new order status called “On Backorder.”
Here’s what each part does:
-
register_post_status('wc-on-backorder', ...)
creates the new order status and gives it a unique identifier ‘wc-on-backorder.’'label'
specifies the name of the status, which is “On Backorder.”'public'
is set to true, meaning this status can be seen by customers on the website.'exclude_from_search'
is set to false, so products with this status will not be excluded from search results.'show_in_admin_all_list'
and'show_in_admin_status_list'
both set to true, which means it will appear in the list of orders in the admin dashboard.'label_count'
provides the text for displaying the count of orders with this status.
- The second function named
show_backorder_order_status
is used to make this new status translatable (it can be displayed in different languages). It takes the existing$order_statuses
array and adds the “On Backorder” status to it, making it available in different languages if needed. - Finally, the code adds an action and filter:
add_action('init', 'add_backorder_order_status')
hooks the first function to the ‘init’ action, so when the website initializes, it adds the “On Backorder” status.add_filter('wc_order_statuses', 'show_backorder_order_status')
hooks the second function to the ‘wc_order_statuses’ filter, allowing it to modify the list of order statuses.
In simple terms, this code creates a new order status called “On Backorder” in a WooCommerce store, making it visible to both customers and administrators. It also ensures that this status can be translated into different languages if needed.
<?php
function add_backorder_order_status() {
register_post_status('wc-on-backorder', array(
'label' => 'On Backorder',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('On Backorder <span class="count">(%s)</span>', 'On Backorder <span class="count">(%s)</span>')
));
}
add_action('init', 'add_backorder_order_status');
function show_backorder_order_status( $order_statuses ) {
$order_statuses['wc-on-backorder'] = _x('On Backorder', 'Order status', 'woocommerce');
return $order_statuses;
}
add_filter('wc_order_statuses', 'show_backorder_order_status');