Orders: Add Custom Parameter to WC Get Orders Query

Adds 'customvar' to wc_get_orders query meta

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

 * Handle a custom 'customvar' query var to get orders with the 'customvar' meta.
 * @param array $query - Args for WP_Query.
 * @param array $query_vars - Query vars from WC_Order_Query.
 * @return array modified $query
 */
function handle_custom_query_var( $query, $query_vars ) {
	if ( ! empty( $query_vars['customvar'] ) ) {
		$query['meta_query'][] = array(
			'key' => 'customvar',
			'value' => esc_attr( $query_vars['customvar'] ),
		);
	}

	return $query;
}
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_query_var', 100, 2 );

/**
* Make a Call to wc_get_orders with Custom Parameter.
*/
function xx_query_test_display(){
  if ( isset( $_GET['xx-query-orders'] ) && !empty( $_GET['xx-query-orders'] )){

    $orders = wc_get_orders( array( 'customvar' => $_GET['xx-query-orders'] ) );

  die();}
}
add_action( 'wp_head', 'xx_query_test_display' );

Still need help? Contact Us Contact Us