From dc635b95617609e564529a4c84d165b94a50259d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 7 Mar 2019 14:46:01 +0000 Subject: [PATCH] Update search_products to use lookup table --- .../data-stores/class-wc-product-data-store-cpt.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 2a37736c79f..755d71c63a0 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1433,7 +1433,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $post_types = $include_variations ? array( 'product', 'product_variation' ) : array( 'product' ); $post_statuses = current_user_can( 'edit_private_products' ) ? array( 'private', 'publish' ) : array( 'publish' ); - $type_join = ''; $type_where = ''; $status_where = ''; $limit_query = ''; @@ -1468,7 +1467,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da foreach ( $search_terms as $search_term ) { $like = '%' . $wpdb->esc_like( $search_term ) . '%'; - $term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( posts.post_excerpt LIKE %s) OR ( posts.post_content LIKE %s ) OR ( postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s ) )", $like, $like, $like, $like ); // @codingStandardsIgnoreLine. + $term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( posts.post_excerpt LIKE %s) OR ( posts.post_content LIKE %s ) OR ( wc_product_meta_lookup.sku LIKE %s ) )", $like, $like, $like, $like ); // @codingStandardsIgnoreLine. $searchand = ' AND '; } @@ -1481,9 +1480,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $search_where = 'AND (' . implode( ') OR (', $search_queries ) . ')'; } - if ( $type && in_array( $type, array( 'virtual', 'downloadable' ), true ) ) { - $type_join = " LEFT JOIN {$wpdb->postmeta} postmeta_type ON posts.ID = postmeta_type.post_id "; - $type_where = " AND ( postmeta_type.meta_key = '_{$type}' AND postmeta_type.meta_value = 'yes' ) "; + if ( 'virtual' === $type ) { + $type_where = ' AND ( wc_product_meta_lookup.virtual = 1 ) '; + } elseif ( 'downloadable' === $type ) { + $type_where = ' AND ( wc_product_meta_lookup.downloadable = 1 ) '; } if ( ! $all_statuses ) { @@ -1498,8 +1498,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $search_results = $wpdb->get_results( // phpcs:disable "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts - LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id - $type_join + LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id WHERE posts.post_type IN ('" . implode( "','", $post_types ) . "') $search_where $status_where