wc_orders_count()
Return the orders count of a specific order status.
No Hooks.
Returns
Int.
Usage
wc_orders_count( $status, $type );
- $status(string) (required)
- Status.
- $type(string)
- (Optional) Order type. Leave empty to include all
'for order-count'order types. @{see wc_get_order_types()}.
Default:''
wc_orders_count() wc orders count code WC 10.7.0
function wc_orders_count( $status, string $type = '' ) {
$count = 0;
$legacy_statuses = array(
OrderStatus::DRAFT,
OrderStatus::TRASH,
);
$status = ( ! in_array( $status, $legacy_statuses, true ) && 0 !== strpos( $status, 'wc-' ) ) ? 'wc-' . $status : $status;
$valid_types = wc_get_order_types( 'order-count' );
$type = trim( $type );
try {
$types_for_count = $type ? array( $type ) : $valid_types;
$order_count_cache = new OrderCountCache();
foreach ( $types_for_count as $type ) {
$cache = $order_count_cache->get( $type, array( $status ) );
if ( false !== $cache && isset( $cache[ $status ] ) ) {
$count += $cache[ $status ];
} else {
$count_for_type = OrderUtil::get_count_for_type( $type );
$count += $count_for_type[ $status ];
}
}
return $count;
} catch ( Exception $e ) {
return 0;
}
}