Automattic\WooCommerce\Admin\API
Products::add_wp_query_filter
Add in conditional search filters for products.
Method of the class: Products{}
No Hooks.
Returns
String.
Usage
$result = Products::add_wp_query_filter( $where, $wp_query );
- $where(string) (required)
- Where clause used to search posts.
- $wp_query(object) (required)
- WP_Query object.
Products::add_wp_query_filter() Products::add wp query filter code WC 10.3.6
public static function add_wp_query_filter( $where, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
if ( $search ) {
$title_like = '%' . $wpdb->esc_like( $search ) . '%';
$where .= $wpdb->prepare( " AND ({$wpdb->posts}.post_title LIKE %s", $title_like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$where .= wc_product_sku_enabled() ? $wpdb->prepare( ' OR wc_product_meta_lookup.sku LIKE %s)', $search ) : ')';
}
if ( $wp_query->get( 'low_in_stock' ) ) {
$low_stock_amount = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
$where .= "
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
AND wc_product_meta_lookup.stock_status IN('instock','outofstock')
AND (
(
low_stock_amount_meta.meta_value > ''
AND wc_product_meta_lookup.stock_quantity <= CAST(low_stock_amount_meta.meta_value AS SIGNED)
)
OR (
(
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}
)
)";
}
return $where;
}