Automattic\WooCommerce\Internal\Admin\Orders
ListTable::get_months_filter_options
Get a list of year-month options for filtering the orders list table.
This finds the oldest order and generates a year-month option for every month in the range between then and the current month.
Method of the class: ListTable{}
No Hooks.
Returns
\stdClass[].
Usage
// protected - for code of main (parent) or child class $result = $this->get_months_filter_options(): array;
ListTable::get_months_filter_options() ListTable::get months filter options code WC 10.3.3
protected function get_months_filter_options(): array {
global $wpdb;
$orders_table = esc_sql( OrdersTableDataStore::get_orders_table_name() );
$min_max_months = $wpdb->get_row(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is escaped above.
$wpdb->prepare(
"
SELECT MIN( t.date_created_gmt ) as min_date_gmt,
MAX( t.date_created_gmt ) as max_date_gmt
FROM `{$orders_table}` t
WHERE type = %s
AND status != %s
",
$this->order_type,
OrderStatus::TRASH
)
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
);
/**
* Normalize "this month" to be the first day of the month in the current timezone of the site.
*/
$this_month = new \WC_DateTime(
'now',
new \DateTimeZone( 'UTC' )
);
$this_month->setTimezone( wp_timezone() );
$this_month->setDate( $this_month->format( 'Y' ), $this_month->format( 'm' ), 1 );
$this_month->setTime( 0, 0 );
$options = array();
if ( isset( $min_max_months ) && ! is_null( $min_max_months->min_date_gmt ) ) {
$start = new \WC_DateTime(
$min_max_months->min_date_gmt,
new \DateTimeZone( 'UTC' )
);
$start->setTimezone( wp_timezone() );
$start->setDate( $start->format( 'Y' ), $start->format( 'm' ), 1 );
$start->setTime( 0, 0 );
$end = new \WC_DateTime(
$min_max_months->max_date_gmt,
new \DateTimeZone( 'UTC' )
);
$end->setTimezone( wp_timezone() );
$end->setDate( $end->format( 'Y' ), $end->format( 'm' ), 1 );
$end->setTime( 0, 0 );
if ( $start > $this_month ) {
$start = $this_month;
}
if ( $end < $this_month ) {
$end = $this_month;
}
$intervals = new \DatePeriod( $start, new \DateInterval( 'P1M' ), $end );
foreach ( $intervals as $interval ) {
$option = new \stdClass();
$option->year = $interval->format( 'Y' );
$option->month = $interval->format( 'n' );
$options[] = $option;
}
$option = new \stdClass();
$option->year = $end->format( 'Y' );
$option->month = $end->format( 'n' );
$options[] = $option;
}
if ( count( $options ) < 1 ) {
$option = new \stdClass();
$option->year = $this_month->format( 'Y' );
$option->month = $this_month->format( 'n' );
$options[] = $option;
}
return array_reverse( $options );
}