Skip to content

Commit

Permalink
Fix for searching orders
Browse files Browse the repository at this point in the history
  • Loading branch information
digamber89 committed May 15, 2024
1 parent ee2d6fb commit c52b4ab
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions includes/AjaxHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ public function get_orders() {
$q = sanitize_text_field( filter_input( INPUT_POST, 'query' ) );
$response = [];

$query = $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND CAST(ID AS CHAR) LIKE %s", $wpdb->esc_like( $q ) . '%' );

$table_name = $wpdb->prefix . 'wc_orders';
$query = $wpdb->prepare("SELECT ID FROM $table_name WHERE type = 'shop_order' AND CAST(ID AS CHAR) LIKE %s", $wpdb->esc_like( $q ) . '%' );

if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
// Table doesn't exist, fallback to {$wpdb->prefix}posts
$table_name = "{$wpdb->prefix}posts";
$query = $wpdb->prepare("SELECT ID FROM $table_name WHERE post_type = 'shop_order' AND CAST(ID AS CHAR) LIKE %s", $wpdb->esc_like( $q ) . '%' );
}


$order_ids = $wpdb->get_col($query);

if ( $order_ids ) {
Expand All @@ -37,6 +47,6 @@ public function get_orders() {
}
wp_reset_postdata();
wp_send_json( $response );
die;
wp_die();
}
}

0 comments on commit c52b4ab

Please sign in to comment.