Hi
This code adds checkbox at bottom – mark it and use standard date range filter.
thanks, Alex
// Filter by custom date field
class Woe_Custom_Date_Filter {
var $field = "order_delivery_date"; // modify it!
var $format = "%Y-%m-%d"; // modify it!
function __construct() {
//add settings
add_action("woe_settings_above_buttons", function ($settings) {
$selected = !empty($settings[ 'filter_by_custom_date_field' ]) ? 'checked': '';
echo '<br><input type=hidden name="settings[filter_by_custom_date_field]" value="0">
<input type=checkbox name="settings[filter_by_custom_date_field]" value="1" '. $selected .'>
<span class="wc-oe-header">Filter by custom field</span>';
});
// set hooks ?
add_filter("woe_settings_validate_defaults", function($settings) {
if( !empty($settings[ 'filter_by_custom_date_field' ]) ) {
$this->original_settings = $settings;
add_filter('woe_sql_get_order_ids_left_joins', function ($joins) {
global $wpdb;
$joins[] = "LEFT JOIN {$wpdb->postmeta} AS custom_date_filter ON (custom_date_filter.post_id = orders.ID AND custom_date_filter.meta_key='{$this->field}')";
return $joins;
});
add_filter( 'woe_sql_get_order_ids_where', function( $where, $settings ) {
if( $this->original_settings['from_date']) {
$where[] = "'" . $this->original_settings['from_date'] ."' <= STR_TO_DATE(custom_date_filter.meta_value, '{$this->format}')";
}
if( $this->original_settings['to_date']) {
$where[] = "STR_TO_DATE(custom_date_filter.meta_value, '{$this->format}') <= '" . $this->original_settings['to_date'] ."'";
}
return $where;
},10,2);
$settings['from_date'] = $settings['to_date'] = "";// don't use default WHERE
}
return $settings;
});
}
}
new Woe_Custom_Date_Filter();
Thank you!
I added this code to one of our plugins, and we were able to export the desired report data!
Very much appreciated!