Filter by date_paid and date_completed on list table orders (#38428)

* Filter by date_paid and date_completed on list table orders based on configuration

* Use separate query param 'order_date_type' to filter orders

* Add clarifying comment

* Add missing phpcs ignore

* Refactor to use 'm' and 'order_date_type' query parameters
This commit is contained in:
Nathan Silveira 2023-05-26 05:19:01 -03:00 committed by GitHub
parent 0cfbf0a653
commit c44ee9649f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import {
REPORTS_STORE_NAME,
SETTINGS_STORE_NAME,
QUERY_DEFAULTS,
OPTIONS_STORE_NAME,
} from '@woocommerce/data';
import {
appendTimestamp,
@ -148,7 +149,7 @@ class RevenueReportTable extends Component {
const orderLink = (
<Link
href={
'edit.php?post_type=shop_order&m=' +
`edit.php?post_type=shop_order&order_date_type=${ this.props.dateType }&m=` +
formatDate( 'Ymd', row.date_start )
}
type="wp-admin"
@ -292,7 +293,7 @@ RevenueReportTable.contextType = CurrencyContext;
* @return {Object} formatted tableData prop
*/
const formatProps = memoize(
( isError, isRequesting, tableQuery, revenueData ) => ( {
( isError, isRequesting, tableQuery, revenueData, dateType ) => ( {
tableData: {
items: {
data: get( revenueData, [ 'data', 'intervals' ], EMPTY_ARRAY ),
@ -302,14 +303,16 @@ const formatProps = memoize(
isRequesting,
query: tableQuery,
},
dateType,
} ),
( isError, isRequesting, tableQuery, revenueData ) =>
( isError, isRequesting, tableQuery, revenueData, dateType ) =>
[
isError,
isRequesting,
new URLSearchParams( tableQuery ).toString(),
get( revenueData, [ 'totalResults' ], 0 ),
get( revenueData, [ 'data', 'intervals' ], EMPTY_ARRAY ).length,
dateType,
].join( ':' )
);
@ -351,6 +354,8 @@ export default compose(
const { woocommerce_default_date_range: defaultDateRange } = select(
SETTINGS_STORE_NAME
).getSetting( 'wc_admin', 'wcAdminSettings' );
const { getOption } = select( OPTIONS_STORE_NAME );
const dateType = getOption( 'woocommerce_date_type' ) || 'date_paid';
const datesFromQuery = getCurrentDates( query, defaultDateRange );
const { getReportStats, getReportStatsError, isResolving } =
select( REPORTS_STORE_NAME );
@ -379,6 +384,12 @@ export default compose(
filteredTableQuery,
] );
return formatProps( isError, isRequesting, tableQuery, revenueData );
return formatProps(
isError,
isRequesting,
tableQuery,
revenueData,
dateType
);
} )
)( RevenueReportTable );

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Filter by date_paid and date_completed on list table orders based on configuration

View File

@ -646,11 +646,11 @@ class WC_Admin_List_Table_Orders extends WC_Admin_List_Table {
public function search_custom_fields( $wp ) {
global $pagenow;
if ( 'edit.php' !== $pagenow || empty( $wp->query_vars['s'] ) || 'shop_order' !== $wp->query_vars['post_type'] || ! isset( $_GET['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'edit.php' !== $pagenow || 'shop_order' !== $wp->query_vars['post_type'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
$post_ids = wc_order_search( wc_clean( wp_unslash( $_GET['s'] ) ) ); // WPCS: input var ok, sanitization ok.
$post_ids = isset( $_GET['s'] ) && ! empty( $wp->query_vars['s'] ) ? wc_order_search( wc_clean( wp_unslash( $_GET['s'] ) ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $post_ids ) ) {
// Remove "s" - we don't want to search order name.
@ -662,5 +662,23 @@ class WC_Admin_List_Table_Orders extends WC_Admin_List_Table {
// Search by found posts.
$wp->query_vars['post__in'] = array_merge( $post_ids, array( 0 ) );
}
if ( isset( $_GET['order_date_type'] ) && isset( $_GET['m'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$date_type = wc_clean( wp_unslash( $_GET['order_date_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$date_query = wc_clean( wp_unslash( $_GET['m'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// date_paid and date_completed are stored in postmeta, so we need to do a meta query.
if ( 'date_paid' === $date_type || 'date_completed' === $date_type ) {
$date_start = \DateTime::createFromFormat( 'Ymd H:i:s', "$date_query 00:00:00" );
$date_end = \DateTime::createFromFormat( 'Ymd H:i:s', "$date_query 23:59:59" );
unset( $wp->query_vars['m'] );
if ( $date_start && $date_end ) {
$wp->query_vars['meta_key'] = "_$date_type"; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$wp->query_vars['meta_value'] = array( strval( $date_start->getTimestamp() ), strval( $date_end->getTimestamp() ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
$wp->query_vars['meta_compare'] = 'BETWEEN';
}
}
}
}
}