Skip to content

Instantly share code, notes, and snippets.

@vishalck
Last active June 24, 2017 11:48
Show Gist options
  • Save vishalck/46fd7ba162912ebc059485b7b70fe677 to your computer and use it in GitHub Desktop.
Save vishalck/46fd7ba162912ebc059485b7b70fe677 to your computer and use it in GitHub Desktop.
wc_orders_count() from includes/wc-order-functions.php file.
<?php
/**
* Return the orders count of a specific order status.
*
* @param string $status
* @return int
*/
function wc_orders_count( $status ) {
$count = 0;
$status = 'wc-' . $status;
$order_statuses = array_keys( wc_get_order_statuses() );
if ( ! in_array( $status, $order_statuses ) ) {
return 0;
}
$cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . $status;
$cached_count = wp_cache_get( $cache_key, 'counts' );
if ( false !== $cached_count ) {
return $cached_count;
}
foreach ( wc_get_order_types( 'order-count' ) as $type ) {
$data_store = WC_Data_Store::load( 'shop_order' === $type ? 'order' : $type );
if ( $data_store ) {
$count += $data_store->get_order_count( $status );
}
}
wp_cache_set( $cache_key, $count, 'counts' );
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment