Snippets

Visser Labs Adding a custom Order filter to the Orders export type

Created by Michael Visser last modified
<?php
function custom_woo_ce_orders_filter_by_custom_meta() {

	// This is the Order meta key we want to filter Orders by
	$meta_key = '_shipping_postcode';

	ob_start(); ?>
<p><label id="order_filter_custom_meta"><?php printf( __( 'Filter Orders by %s', 'woocommerce-exporter' ), $meta_key ); ?></label></p>
<div id="export-orders-filters-custom_meta" class="separator">
	<ul>
		<li>
			<input type="text" id="order_filter_custom_meta" name="order_filter_custom_meta" />
		</li>
	</ul>
</div>
<!-- #export-orders-filters-custom_meta -->
<?php
	ob_end_flush();

}
add_action( 'woo_ce_export_order_options_before_table', 'custom_woo_ce_orders_filter_by_custom_meta', 11 );

function custom_woo_ce_get_orders_args( $args ) {

	// This is the Order meta key we want to filter Orders by
	$meta_key = '_shipping_postcode';

	// This is the Order meta value we will pass to WP_Query to return the filtered Orders
	$custom_meta = ( isset( $_POST['order_filter_custom_meta'] ) ? sanitize_text_field( $_POST['order_filter_custom_meta'] ) : false );
	if( !empty( $custom_meta ) ) {

		// Check if $args['meta_query'] exists
		if( !isset( $args['meta_query'] ) )
			$args['meta_query'] = array();

		// Add our custom meta
		$args['meta_query'][] = array(
			'key' => $meta_key,
			'value' => $custom_meta
		);

	}
	return $args;

}
add_filter( 'woo_ce_get_orders_args', 'custom_woo_ce_get_orders_args' );
?>

Comments (0)