• Resolved Marion Dorsett

    (@mariondorsett)


    Is it possible to export orders between dates based on a custom field?

    If we use the Filter by order > Custom Fields and we choose:

    custom_field_date > YYYY-MM-DD

    It will work, but we can’t get the range we need:

    custom_field_date BETWEEN YYYY-MM-DD AND YYYY-MM-DD

    The short version is that we have an order_delivery_date custom field, and that’s what’s used to fulfill production, and we need to export orders based on the desired date range vs the default dates.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author algol.plus

    (@algolplus)

    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();
    Thread Starter Marion Dorsett

    (@mariondorsett)

    Thank you!

    I added this code to one of our plugins, and we were able to export the desired report data!

    Very much appreciated!

    Plugin Author algol.plus

    (@algolplus)

    you’re welcome

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Filter between dates in custom field’ is closed to new replies.