Skip to content

Instantly share code, notes, and snippets.

@rashedripon

rashedripon/functions.php

Last active Mar 1, 2021
Embed
What would you like to do?
Set custom order status for backorder items
// ---------------------
// 1. Register Order Status
add_filter( 'woocommerce_register_shop_order_post_statuses', 'extended_register_custom_order_status' );
function extended_register_custom_order_status( $order_statuses ){
// Status must start with "wc-"
$order_statuses['wc-custom-status'] = array(
'label' => _x( 'Custom Status', 'Order status', 'woocommerce' ),
'public' => false,
'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' ),
);
return $order_statuses;
}
// ---------------------
// 2. Show Order Status in the Dropdown @ Single Order and "Bulk Actions" @ Orders
add_filter( 'wc_order_statuses', 'extended_show_custom_order_status' );
function extended_show_custom_order_status( $order_statuses ) {
$order_statuses['wc-custom-status'] = _x( 'Custom Status', 'Order status', 'woocommerce' );
return $order_statuses;
}
add_filter( 'bulk_actions-edit-shop_order', 'extended_get_custom_order_status_bulk' );
function extended_get_custom_order_status_bulk( $bulk_actions ) {
// Note: "mark_" must be there instead of "wc"
$bulk_actions['mark_custom-status'] = 'Change status to custom status';
return $bulk_actions;
}
// ---------------------
// 3. Set Custom Order Status @ WooCommerce Checkout Process
add_action( 'woocommerce_thankyou', 'extended_thankyou_change_order_status' );
function extended_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;
// Get a an instance of order object
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ) {
// Get a an instance of product object related to the order item
$product = $item->get_product();
// Check if the product is on backorder
if( $product->is_on_backorder() ){
// Change this order status
$order->update_status( 'custom-status' );
break; // Stop the loop
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment