Update stock and sku search query in API to use lookup table.
Lookup table was introduced in 3.6 and will improve performance for these 2 queries.
This commit is contained in:
parent
684c526d1e
commit
f85b25f34d
|
@ -124,24 +124,24 @@ class WC_Admin_REST_Products_Controller extends WC_REST_Products_Controller {
|
|||
$search = $wpdb->esc_like( $search );
|
||||
$search = "'%" . $search . "%'";
|
||||
$where .= " AND ({$wpdb->posts}.post_title LIKE {$search}";
|
||||
$where .= wc_product_sku_enabled() ? ' OR ps_post_meta.meta_key = "_sku" AND ps_post_meta.meta_value LIKE ' . $search . ')' : ')';
|
||||
$where .= wc_product_sku_enabled() ? ' OR wc_product_meta_lookup.sku LIKE ' . $search . ')' : ')';
|
||||
}
|
||||
|
||||
if ( $wp_query->get( 'low_in_stock' ) ) {
|
||||
$low_stock_amount = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
|
||||
$where .= "
|
||||
AND manage_stock_meta.meta_value = 'yes'
|
||||
AND stock_meta.meta_value IS NOT NULL
|
||||
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
|
||||
AND (
|
||||
(
|
||||
low_stock_amount_meta.meta_value > ''
|
||||
AND CAST(stock_meta.meta_value AS SIGNED) <= CAST(stock_meta.meta_value AS SIGNED)
|
||||
AND wc_product_meta_lookup.stock_quantity <= CAST(low_stock_amount_meta.meta_value AS SIGNED)
|
||||
)
|
||||
OR (
|
||||
low_stock_amount_meta.meta_value <= ''
|
||||
AND CAST(stock_meta.meta_value AS SIGNED) <= {$low_stock_amount}
|
||||
(
|
||||
low_stock_amount_meta.meta_value IS NULL OR low_stock_amount_meta.meta_value <= ''
|
||||
)
|
||||
AND wc_product_meta_lookup.stock_quantity <= {$low_stock_amount}
|
||||
)
|
||||
OR low_stock_amount_meta.meta_value IS NULL
|
||||
)";
|
||||
}
|
||||
|
||||
|
@ -160,19 +160,32 @@ class WC_Admin_REST_Products_Controller extends WC_REST_Products_Controller {
|
|||
|
||||
$search = $wp_query->get( 'search' );
|
||||
if ( $search && wc_product_sku_enabled() ) {
|
||||
$join .= " INNER JOIN {$wpdb->postmeta} AS ps_post_meta ON ps_post_meta.post_id = {$wpdb->posts}.ID";
|
||||
$join = self::append_product_sorting_table_join( $join );
|
||||
}
|
||||
|
||||
if ( $wp_query->get( 'low_in_stock' ) ) {
|
||||
$join .= " INNER JOIN {$wpdb->postmeta} AS stock_meta ON {$wpdb->posts}.ID = stock_meta.post_id AND stock_meta.meta_key = '_stock'
|
||||
INNER JOIN {$wpdb->postmeta} AS manage_stock_meta ON {$wpdb->posts}.ID = manage_stock_meta.post_id AND manage_stock_meta.meta_key = '_manage_stock'
|
||||
LEFT JOIN {$wpdb->postmeta} AS low_stock_amount_meta ON {$wpdb->posts}.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount'
|
||||
";
|
||||
$join = self::append_product_sorting_table_join( $join );
|
||||
$join .= " LEFT JOIN {$wpdb->postmeta} AS low_stock_amount_meta ON {$wpdb->posts}.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount' ";
|
||||
}
|
||||
|
||||
return $join;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join wc_product_meta_lookup to posts if not already joined.
|
||||
*
|
||||
* @param string $sql SQL join.
|
||||
* @return string
|
||||
*/
|
||||
protected static function append_product_sorting_table_join( $sql ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {
|
||||
$sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Group by post ID to prevent duplicates.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue