Add get_product_by_metas method to query the lookup table (https://github.com/woocommerce/woocommerce-blocks/pull/8645)
* Add get_product_by_metas method to query the lookup table * Remove cache and return db column instead * Update src/StoreApi/Utilities/ProductQueryFilters.php Co-authored-by: Patricia Hillebrandt <patriciahillebrandt@gmail.com> * Fix phpcs violations --------- Co-authored-by: Patricia Hillebrandt <patriciahillebrandt@gmail.com>
This commit is contained in:
parent
ef63d807b9
commit
a55b63f96c
|
@ -212,6 +212,54 @@ class ProductQueryFilters {
|
|||
return array_map( 'absint', wp_list_pluck( $results, 'product_count', 'rounded_average_rating' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets product by metas.
|
||||
*
|
||||
* @since TBD
|
||||
* @param array $metas Array of metas to query.
|
||||
* @return array $results
|
||||
*/
|
||||
public function get_product_by_metas( $metas = array() ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( empty( $metas ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$where = array();
|
||||
$results = array();
|
||||
$params = array();
|
||||
|
||||
foreach ( $metas as $column => $value ) {
|
||||
if ( 'min_price' === $column ) {
|
||||
$where[] = "{$column} >= %f";
|
||||
$params[] = (float) $value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'max_price' === $column ) {
|
||||
$where[] = "{$column} <= %f";
|
||||
$params[] = (float) $value;
|
||||
continue;
|
||||
}
|
||||
|
||||
$where[] = "{$column} = %s";
|
||||
$params[] = $value;
|
||||
}
|
||||
|
||||
if ( ! empty( $where ) ) {
|
||||
$where_clause = implode( ' AND ', $where );
|
||||
// Use a parameterized query.
|
||||
$results = $wpdb->get_col(
|
||||
$wpdb->prepare( "SELECT DISTINCT product_id FROM {$wpdb->prefix}wc_product_meta_lookup WHERE {$where_clause}", // phpcs:ignore
|
||||
$params
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets product by filtered terms.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue