Snippets

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

Updated by Michael Visser

File functions.php Modified

  • Ignore whitespace
  • Hide word diff
 	$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>
+<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();
 
Updated by Michael Visser

File functions.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
-function woo_ce_scheduled_export_orders_filter_by_custom_meta( $post_ID = 0 ) {
+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';
 	ob_end_flush();
 
 }
-add_action( 'woo_ce_after_scheduled_export_type_options', 'woo_ce_scheduled_export_orders_filter_by_custom_meta' );
+add_action( 'woo_ce_after_scheduled_export_type_options', 'custom_woo_ce_scheduled_export_orders_filter_by_custom_meta' );
 
-function woo_ce_scheduled_export_save_order_custom_meta( $post_ID = 0 ) {
+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', 'woo_ce_scheduled_export_save_order_custom_meta' );
+add_action( 'woo_ce_extend_scheduled_export_save', 'custom_woo_ce_scheduled_export_save_order_custom_meta' );
 
-function woo_ce_sceheduled_export_get_orders_args( $args ) {
+function custom_woo_ce_sceheduled_export_get_orders_args( $args ) {
 
 	global $export;
 
 	return $args;
 
 }
-add_filter( 'woo_ce_get_orders_args', 'woo_ce_sceheduled_export_get_orders_args' );
+add_filter( 'woo_ce_get_orders_args', 'custom_woo_ce_sceheduled_export_get_orders_args' );
 ?>
Created by Michael Visser

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+function 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', 'woo_ce_scheduled_export_orders_filter_by_custom_meta' );
+
+function 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', 'woo_ce_scheduled_export_save_order_custom_meta' );
+
+function 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', 'woo_ce_sceheduled_export_get_orders_args' );
+?>