Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/75645825a762dbb97c5e47288b45cb98 to your computer and use it in GitHub Desktop.
Save FrancoStino/75645825a762dbb97c5e47288b45cb98 to your computer and use it in GitHub Desktop.
Add a dropdown to filter orders by date range into shop order list admin - Woocommerce
<?php
// Add a dropdown to filter orders by date range
add_action('restrict_manage_posts', 'add_shop_order_filter_by_date');
function add_shop_order_filter_by_date(){
global $pagenow, $typenow;
if( 'shop_order' === $typenow && 'edit.php' === $pagenow ) {
?>
<script>
jQuery(document).ready(function($) {
$( '#ant_filter_start_date' ).datepicker({
dateFormat: 'yy/mm/dd',
maxDate: '0',
isRTL: <?php echo ( is_rtl() ? 'true' : 'false' ) ?>,
onSelect: function (date) {
var date2 = $('#ant_filter_start_date').datepicker('getDate');
date2.setDate(date2.getDate());
$('#ant_filter_end_date').datepicker('option', 'minDate', date2);
}
});
$( '#ant_filter_end_date' ).datepicker({
dateFormat:'yy/mm/dd',
maxDate: '0',
isRTL: <?php echo ( is_rtl() ? 'true' : 'false' ) ?>
});
});
</script>
<style>
select#filter-by-date {
display: none !important;
}
</style>
<?php
$from = ( isset($_GET['ant_filter_start_date']) ) ? 'Dal giorno: '.sanitize_text_field( $_GET['ant_filter_start_date'] ) : '';
$to = ( isset($_GET['ant_filter_end_date']) ) ? ' | Al giorno: '.sanitize_text_field( $_GET['ant_filter_end_date'] ) : '';
echo'<input type="text" id="ant_filter_start_date" name="ant_filter_start_date" value="'.$from.'" placeholder="'.__( 'Dal giorno', 'woaf-plugin' ).'">';
echo '<input type="text" id="ant_filter_end_date" value="'.$to.'" name="ant_filter_end_date" placeholder="'.__( 'Al giorno', 'woaf-plugin' ).'">';
}
}
// Process the filter dropdown for orders by date range
add_filter( 'pre_get_posts', 'woaf_filter_date_range' );
function woaf_filter_date_range( $wp_query ) {
global $pagenow;
if (
is_admin()
&& $wp_query->is_main_query()
&& isset($_GET['post_type']) && sanitize_text_field($_GET['post_type']) =='shop_order'
&& ! empty( $_GET['ant_filter_start_date'] )
&& ! empty( $_GET['ant_filter_end_date'] )
) {
$from = explode( '/', sanitize_text_field( $_GET['ant_filter_start_date'] ) );
$to = explode( '/', sanitize_text_field( $_GET['ant_filter_end_date'] ) );
$from = array_map( 'intval', $from );
$to = array_map( 'intval', $to );
if (
3 === count( $to )
&& 3 === count( $from )
) {
list( $year_from, $month_from, $day_from ) = $from;
list( $year_to, $month_to, $day_to ) = $to;
} else {
return $wp_query;
}
$wp_query->set(
'date_query',
array(
'after' => array(
'year' => $year_from,
'month' => $month_from,
'day' => $day_from,
),
'before' => array(
'year' => $year_to,
'month' => $month_to,
'day' => $day_to,
),
'inclusive' => apply_filters( 'woo_orders_filterby_date_range_query_is_inclusive', true ),
'column' => apply_filters( 'woo_orders_filterby_date_query_column', 'post_date' ),
)
);
}
return $wp_query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment