How to get slugs and names of all order statuses in WooCommerce?
Asked Answered
U

3

8

I would like to get all slugs and names of all the WooCommerce order statuses. I tried the answers from the following thread: Woocommerce get list of order statuses list but with no success.

I use latest woocommerce version. Any help is appreciated.

Uxorial answered 22/1, 2020 at 16:13 Comment(0)
H
18

You will use the dedicated function wc_get_order_statuses(), from WC_Order functions, which give you by default the following array:

$order_statuses = array(
    'wc-pending'    => _x( 'Pending payment', 'Order status', 'woocommerce' ),
    'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
    'wc-on-hold'    => _x( 'On hold', 'Order status', 'woocommerce' ),
    'wc-completed'  => _x( 'Completed', 'Order status', 'woocommerce' ),
    'wc-cancelled'  => _x( 'Cancelled', 'Order status', 'woocommerce' ),
    'wc-refunded'   => _x( 'Refunded', 'Order status', 'woocommerce' ),
    'wc-failed'     => _x( 'Failed', 'Order status', 'woocommerce' ),
);

All custom additional order statuses will be included too as the filter hook wc_order_statuses is applied inside this function.

Herzel answered 22/1, 2020 at 23:57 Comment(0)
O
-1

Add below snippet to functions.php and call woocommerce_get_order_statuses() where you want to use.

function woocommerce_get_order_statuses() {
      $order_statuses = get_terms( 'shop_order_status', array( 'hide_empty' => false ) );
      $statuses = array();
      foreach ( $order_statuses as $status ) {
        $statuses[ $status->slug ] = $status->name;
      }
      return $statuses;
    }
Oloroso answered 22/1, 2020 at 16:44 Comment(0)
F
-2

wc_get_order_statuses() will provide only default WooCommerce statuses but to get all order statuses you may use the following function woocommerce_get_all_order_statuses()

function woocommerce_get_all_order_statuses() {
  $order_statuses = get_posts( array('post_type'=>'wc_order_status', 'post_status'=>'publish', 'numberposts'=>-1) );
 
  $statuses = array();
  foreach ( $order_statuses as $status ) {
    $statuses[ $status->post_name ] = $status->post_title;
  }

  return $statuses;
}
Femoral answered 26/7, 2021 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.