Filter orders
While the UI provides robust filtering by date, status, product, customer, coupon, and payment method, some business rules require deeper customisation. This guide explores the developer hooks that allow you to modify the plugin’s order selection logic programmatically — enabling you to filter, add, or remove orders from any export run.
Overview of Order Filtering Hooks
The plugin provides three primary hooks for customising order selection:
|
Hook |
Purpose |
When It Fires |
|---|---|---|
|
|
Filter individual orders before export |
After order IDs are selected, before export begins |
|
|
Modify the complete list of order IDs |
During the order selection process |
|
|
Inject custom SQL conditions into the WHERE clause |
When building the query to fetch order IDs |
These hooks are fired in sequence during any export — manual, scheduled, or status‑change triggered. By attaching custom functions to them, you gain fine‑grained control over exactly which orders appear in your export files.
Example 1: Exclude Low‑Value Orders
This example excludes any order with a total of €10 or less. Use it when your downstream system requires a minimum order value, preventing empty or trivial records from being exported.
|
1 2 3 4 5 |
// export only orders with total > 10 add_filter( 'woe_order_export_started', function ( $order_id ) { $order = new WC_Order($order_id); return ($order->get_total() > 10.00) ? $order_id: false; }); |
Example 2: Skip WooCommerce Subscription Renewal Orders
If your store uses WooCommerce Subscriptions and you want to export only the original subscription orders (excluding automated renewal orders), this filter inspects the order type using the wcs_order_contains_subscription function.
|
1 2 3 4 5 6 7 8 9 |
// skip renewal orders generated by WooCommerce Subscription add_filter('woe_order_export_started', function($order_id){ if( !function_exists("wcs_order_contains_subscription") ) return $order_id; if( !wcs_order_contains_subscription($order_id,'renewal') ) return $order_id; else return false; }); |
Example 3: Include Order Refunds
By default, the plugin exports only shop_order post type entries. This example demonstrates how to also include shop_order_refund records, ensuring that refund transactions appear alongside their parent orders.
|
1 2 3 4 5 6 7 8 |
//add refunds to order export add_filter( "woe_get_order_ids", function($ids) { global $wpdb; if(!$ids) return $ids; $ids_sql = join(",",$ids); $refunds = $wpdb->get_col("SELECT ID AS order_id FROM {$wpdb->prefix}posts WHERE post_type='shop_order_refund' AND post_parent in ($ids_sql) "); return array_merge($ids,$refunds); }); |
Example 4: Export Only Orders That Contain Customer Notes
This example appends a condition to include only orders that have non‑empty customer notes (post excerpt).
|
1 2 3 4 5 |
//export orders having non-empty customer notes add_filter( 'woe_sql_get_order_ids_where', function($where, $settings) { $where[] = " orders.post_excerpt<>'' "; return $where; },10,2); |
Example 5: Replace {yesterday} and {today} Placeholders
This example demonstrates how the plugin replaces {yesterday} and {today} with actual date strings.
|
1 2 3 4 5 6 7 8 9 10 11 |
// replace {yesterday} and {today} with actual values comparison operators // tweak formats for your needs! add_filter('woe_settings_validate_defaults', function ($settings) { $settings = json_encode($settings); // to string $yesterday = date("Y-m-d" , strtotime("-1 day", current_time( 'timestamp' ) )); $settings = str_replace( '{yesterday}', $yesterday, $settings); $today = date("Y-m-d" , current_time( 'timestamp' ) ); $settings = str_replace( '{today}', $today, $settings); $settings = json_decode($settings, true); // to array return $settings; } ); |
Example 6: Replace {current_user} Placeholder
This example replaces {current_user} with the current logged‑in user’s ID, enabling user‑specific filtering.
|
1 2 3 4 5 6 7 8 |
// replace {current_user} with actual value in comparison operators add_filter('woe_settings_validate_defaults', function ($settings) { $settings = json_encode($settings); // to string $user_id = get_current_user_id(); $settings = str_replace( '{current_user}', $user_id, $settings); $settings = json_decode($settings, true); // to array return $settings; } ); |
Example 7: Additional Filters for Row‑Level Data
While not directly related to order selection, these filters allow filtering at the data‑row level and can be useful in conjunction with the order selection hooks.
woe_fetch_order_coupon – Filter Individual Coupon Rows
This filter is called for each coupon applied to an order. Returning false excludes that coupon row from the export.
|
1 2 3 4 5 6 7 |
//skip zero coupons add_filter( 'woe_fetch_order_coupon', function($row, $item, $item_meta) { if ( $item['discount_amount'] == 0 ) return false; else return $row; },10,4); |
Example 8: Setting a Custom Time Range
For scheduled exports that require a non‑standard date range, you can programmatically override the from_date and to_date settings. The example below sets a range from 19:00 the previous day to 12:00 today.
|
1 2 3 4 5 6 7 |
// set custom time range in scheduled job // export orders starting from the last order of 19 of yesterday until 12 today add_filter('woe_settings_validate_defaults', function($settings) { $settings['from_date'] = date( "Y-m-d 19:00:00", strtotime( "-1 day",current_time("timestamp")) ); $settings['to_date'] = date( "Y-m-d 12:00:00", current_time("timestamp")); return $settings; }); |
Example 9: Trigger Pending Order Export on Checkout
When a customer completes checkout, the order is saved with the pending status, and this code triggers the plugin’s status change export for that order.
|
1 2 3 4 |
//export pending orders by Status Change job add_action( 'woocommerce_checkout_order_processed', function($order_id, $posted_data, $order ){ do_action("woocommerce_order_status_changed",$order_id,"","wc-pending"); },10,3); |