Registering a post status and adding it to WooCommerce

The first function we called should register a post status. Remember, this is what tracks the status of an order. So we need to add a new post status so we can select it from a dropdown.

This is pretty easy to do if you read through the documentation on the WordPress website about post statuses, available at https://developer.wordpress.org/reference/functions/register_post_status/. It only takes a few lines, as you can see here:

function register_building_order_status() {
register_post_status( 'wc-building', array(
'label' => 'Building',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Building <span class="count">(%s)</span>',
'Building <span class="count">(%s)</span>' )
) );
}

With this function, we created a new post status called Building and we also hid it from searches and from a few other areas in your WordPress site. Your site doesn't know when or where it should show that post status. 

Now we need to tell WooCommerce when to display this new order status. We'll loop through each of the existing order statuses in WooCommerce and add our new order status right after wc-processing, which is the Processing status:

function add_building_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-building'] = 'Building';
}
}
return $new_order_statuses;
}

With this code, we're looping through all of the order statuses. If we find one called wc-processing, we add a new order status called Building.

When you're done, save your files, make sure your plugin is activated in the backend, and then open up an order. You should see your new order status, as we can see in the following screenshot:

Now we can change any order to the Building status. This will help us track the status of orders. In this example, we added one order status, but you could add as many as you wanted to help you track your orders.

Next up, we will look at building integration with WooCommerce.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.61.119