Snippets

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

Created by Michael Visser

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+function custom_woo_ce_scheduled_export_products_filter_by_custom_meta( $post_ID = 0 ) {
+
+	// This is the Product meta key we want to filter Products by
+	$meta_key = 'performance_date';
+	$custom_meta = get_post_meta( $post_ID, '_filter_product_custom_meta', true );
+
+	ob_start(); ?>
+<div class="export-options product-options">
+	<p class="form-field discount_type_field">
+		<label for="product_filter_custom_meta"><?php echo esc_attr( $meta_key ); ?></label></label>
+		<input type="text" id="product_filter_custom_meta" name="product_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_products_filter_by_custom_meta' );
+
+function custom_woo_ce_scheduled_export_save_product_custom_meta( $post_ID = 0 ) {
+
+	// Save the Post meta value against the Scheduled Export
+	update_post_meta( $post_ID, '_filter_product_custom_meta', ( isset( $_POST['product_filter_custom_meta'] ) ? sanitize_text_field( $_POST['product_filter_custom_meta'] ) : false ) );
+
+}
+add_action( 'woo_ce_extend_scheduled_export_save', 'custom_woo_ce_scheduled_export_save_product_custom_meta' );
+
+function custom_woo_ce_scheduled_export_get_products_args( $args ) {
+
+	global $export;
+
+	// Check if this is a Export
+	if( !isset( $export ) )
+		return $args;
+
+	// 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 Product meta key we want to filter Products by
+	$meta_key = 'performance_date';
+	$custom_meta = get_post_meta( $scheduled_export, '_filter_product_custom_meta', true );
+
+	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_products_args', 'custom_woo_ce_scheduled_export_get_products_args' );
+?>
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.