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:
Roy Ho 2023-03-15 05:32:25 -07:00 committed by GitHub
parent ef63d807b9
commit a55b63f96c
1 changed files with 48 additions and 0 deletions

View File

@ -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.
*