Snippets

Visser Labs Adding a custom Order filter to the Edit Scheduled Export screen

Created by Michael Visser last modified
<?php
function custom_woo_ce_scheduled_export_orders_filter_by_custom_meta( $post_ID = 0 ) {

	// This is the Order meta key we want to filter Orders by, it also forms part of the Filter Orders by... label
	$meta_key = '_shipping_postcode';

	// This is the saved Order meta value we want to filter Orders by
	$custom_meta = get_post_meta( $post_ID, '_filter_order_custom_meta', true );

	ob_start(); ?>
<div class="export-options order-options">
	<p class="form-field discount_type_field">
		<label for="order_filter_custom_meta"><?php printf( __( 'Filter Orders by %s', 'woocommerce-exporter' ), $meta_key ); ?></label></label>
		<input type="text" id="order_filter_custom_meta" name="order_filter_custom_meta" value="<?php echo $custom_meta; ?>" size="5" class="text" />
	</p>
</div>
<?php
	ob_end_flush();

}
add_action( 'woo_ce_after_scheduled_export_type_options', 'custom_woo_ce_scheduled_export_orders_filter_by_custom_meta' );

function custom_woo_ce_scheduled_export_save_order_custom_meta( $post_ID = 0 ) {

	// Save the Post meta value against the Scheduled Export
	update_post_meta( $post_ID, '_filter_order_custom_meta', ( isset( $_POST['order_filter_custom_meta'] ) ? sanitize_text_field( $_POST['order_filter_custom_meta'] ) : false ) );

}
add_action( 'woo_ce_extend_scheduled_export_save', 'custom_woo_ce_scheduled_export_save_order_custom_meta' );

function custom_woo_ce_sceheduled_export_get_orders_args( $args ) {

	global $export;

	// Check that this is a Scheduled Export
	$scheduled_export = ( $export->scheduled_export ? absint( get_transient( WOO_CD_PREFIX . '_scheduled_export_id' ) ) : 0 );
	if( empty( $scheduled_export ) )
		return $args;

	// This is the Order meta key and meta value we want to filter Orders by
	$meta_key = '_shipping_postcode';
	$custom_meta = get_post_meta( $scheduled_export, '_filter_order_custom_meta', true );

	// 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_sceheduled_export_get_orders_args' );
?>

Comments (0)