Per-request cache of get_terms() results keyed by taxonomy. */ private $terms_cache = []; public function find( $message, array $query_plan = [] ) { $message = sanitize_text_field( (string) $message ); $message_lower = strtolower( $message ); $filters = $this->extract_filters( $message, $message_lower, $query_plan ); $strict_matches = []; $has_search = $filters['search'] !== ''; $compare_items = $this->extract_compare_items( $message, $query_plan ); if ( 'compare' === $filters['intent'] && count( $compare_items ) >= 2 ) { $products = $this->find_products_for_comparison( $compare_items, $filters ); return [ 'products' => $products, 'total_found' => count( $products ), 'applied_filters' => $this->build_applied_filters_payload( $filters, $compare_items ), 'show_more_url' => '', 'query_args' => [], ]; } if ( ! $filters['should_lookup'] ) { return [ 'products' => [], 'total_found' => 0, 'applied_filters'=> $this->build_applied_filters_payload( $filters ), 'show_more_url' => '', 'query_args' => [], ]; } $query_args = [ 'limit' => 60, 'status' => 'publish', 'return' => 'objects', ]; if ( $filters['search'] !== '' ) { $query_args['s'] = $filters['search']; } if ( null !== $filters['max_price'] ) { $query_args['max_price'] = (string) $filters['max_price']; } if ( null !== $filters['min_price'] ) { $query_args['min_price'] = (string) $filters['min_price']; } if ( $filters['in_stock_only'] ) { $query_args['stock_status'] = 'instock'; } if ( $filters['sort_by'] === 'price_asc' || $filters['intent'] === 'lowest_price' ) { $query_args['orderby'] = 'price'; $query_args['order'] = 'ASC'; } elseif ( $filters['sort_by'] === 'price_desc' || $filters['intent'] === 'most_expensive' ) { $query_args['orderby'] = 'price'; $query_args['order'] = 'DESC'; } elseif ( $filters['sort_by'] === 'rating_desc' || $filters['intent'] === 'most_reviewed' ) { $query_args['orderby'] = 'rating'; $query_args['order'] = 'DESC'; } if ( $has_search ) { $strict_matches = $this->find_strict_search_matches( $filters['search'], $filters ); } $products = ! empty( $strict_matches ) ? $strict_matches : wc_get_products( $query_args ); $products = $this->apply_filters( $products, $filters ); if ( empty( $products ) && ! $has_search ) { $products = $this->fallback_price_candidates( $filters ); } if ( $has_search && empty( $strict_matches ) ) { $products = $this->prefer_exact_name_matches( $products, $filters['search'] ); } if ( $has_search ) { $products = $this->filter_search_results_to_query( $products, $filters['search'] ); } if ( ! empty( $filters['category'] ) ) { $products = $this->filter_by_term( $products, 'product_cat', $filters['category'] ); } if ( ! empty( $filters['tag'] ) ) { $products = $this->filter_by_term( $products, 'product_tag', $filters['tag'] ); } $products = $this->rank_products( $products, $filters, $message_lower ); $max_products = $this->get_max_products_to_show( $filters['browse'] ); $total_found = count( $products ); // Always show shop link when products are found $show_more_url = ! empty( $products ) ? $this->resolve_show_more_url( $filters, $message_lower ) : ''; $products = array_slice( $products, 0, $max_products ); return [ 'products' => $products, 'total_found' => $total_found, 'applied_filters' => $this->build_applied_filters_payload( $filters ), 'show_more_url' => $show_more_url, 'query_args' => $query_args, ]; } private function extract_filters( $message, $message_lower, array $query_plan ) { $intent = $this->detect_intent( $message_lower ); if ( isset( $query_plan['intent'] ) && is_string( $query_plan['intent'] ) && $query_plan['intent'] !== '' ) { $planner_intent = sanitize_text_field( strtolower( (string) $query_plan['intent'] ) ); if ( 'none' !== $planner_intent ) { $intent = $planner_intent; } } $search = isset( $query_plan['search'] ) && is_string( $query_plan['search'] ) ? sanitize_text_field( $query_plan['search'] ) : ''; if ( $search === '' ) { $search = $this->extract_search_keywords( $message_lower ); } $browse_request = $this->looks_like_browse_products_request( $message_lower ); if ( $browse_request ) { $search = ''; } $max_price = isset( $query_plan['max_price'] ) && is_numeric( $query_plan['max_price'] ) ? (float) $query_plan['max_price'] : $this->extract_max_price( $message ); $min_price = $this->extract_min_price( $message ); $in_stock_only = ! empty( $query_plan['in_stock_only'] ) || strpos( $message_lower, 'in stock' ) !== false || strpos( $message_lower, 'available' ) !== false || strpos( $message_lower, 'only in stock' ) !== false; $sort_by = isset( $query_plan['sort_by'] ) && is_string( $query_plan['sort_by'] ) ? sanitize_text_field( $query_plan['sort_by'] ) : 'none'; $category = $this->extract_matching_term_slug( 'product_cat', $message_lower ); $tag = $this->extract_matching_term_slug( 'product_tag', $message_lower ); $should_lookup = $browse_request || $search !== '' || null !== $min_price || null !== $max_price || $in_stock_only || $intent !== 'none' || $category !== '' || $tag !== ''; return [ 'intent' => $intent, 'search' => $search, 'min_price' => $min_price, 'max_price' => $max_price, 'in_stock_only' => $in_stock_only, 'sort_by' => $sort_by, 'category' => $category, 'tag' => $tag, 'browse' => $browse_request, 'should_lookup' => $should_lookup, ]; } private function detect_intent( $message_lower ) { if ( preg_match( '/\b(compare|comparison|vs|versus)\b/i', $message_lower ) || preg_match( '/\bprice\s+comparison\b/i', $message_lower ) ) { return 'compare'; } if ( preg_match( '/(most\s+reviewed|top\s+reviewed|highest\s+rated|best\s+rated)/i', $message_lower ) ) { return 'most_reviewed'; } if ( preg_match( '/\b(lowest|cheapest|least\s+expensive|lowest\s+priced|cheapest\s+product|minimum)\b/i', $message_lower ) || preg_match( '/\b(lowest|cheapest|least\s+expensive|minimum).*(price|pricing|cost)\b/i', $message_lower ) ) { return 'lowest_price'; } if ( preg_match( '/\b(most\s+expensive|highest\s+priced|most\s+costly|costliest|priciest|maximum\s+price|highest\s+price)\b/i', $message_lower ) ) { return 'most_expensive'; } return 'none'; } private function extract_compare_items( $message, array $query_plan = [] ) { $items = []; if ( isset( $query_plan['compare_items'] ) && is_array( $query_plan['compare_items'] ) ) { foreach ( array_slice( $query_plan['compare_items'], 0, 2 ) as $item ) { $clean = $this->normalize_compare_phrase( (string) $item ); if ( strlen( $clean ) >= 2 ) { $items[] = $clean; } } } if ( count( $items ) >= 2 ) { return $items; } if ( preg_match( '/(?:compare|comparison|price\s+comparison)\s+(.+?)\s+(?:and|with|vs|versus|between)\s+(.+)/i', $message, $matches ) ) { $left = $this->normalize_compare_phrase( (string) $matches[1] ); $right = $this->normalize_compare_phrase( (string) $matches[2] ); if ( strlen( $left ) >= 2 ) { $items[] = $left; } if ( strlen( $right ) >= 2 ) { $items[] = $right; } } return array_slice( array_values( array_unique( $items ) ), 0, 2 ); } private function normalize_compare_phrase( $phrase ) { $phrase = strtolower( sanitize_text_field( (string) $phrase ) ); if ( '' === $phrase ) { return ''; } $stop_words = [ 'compare', 'comparison', 'price', 'prices', 'cost', 'costs', 'of', 'the', 'a', 'an', 'and', 'with', 'between', 'vs', 'versus', 'for', 'product', 'products', 'item', 'items', 'show', 'me', 'what', 'is', 'are', 'to', 'in', 'on', 'site', 'store', 'this', 'these', 'those', 'most', 'least', 'expensive', 'cheap', 'cheapest', 'lowest', 'highest', 'priced' ]; $words = preg_split( '/[\s\-_,\.!?]+/', trim( $phrase ), -1, PREG_SPLIT_NO_EMPTY ); $keywords = []; foreach ( $words as $word ) { $word = trim( $word ); if ( '' === $word ) { continue; } if ( preg_match( '/^[0-9]+(?:\.[0-9]+)?$/', $word ) ) { $keywords[] = $word; continue; } if ( strlen( $word ) > 1 && ! in_array( $word, $stop_words, true ) ) { $keywords[] = $word; } } return trim( implode( ' ', array_slice( $keywords, 0, 4 ) ) ); } private function find_products_for_comparison( array $compare_items, array $filters ) { $products = []; foreach ( array_slice( $compare_items, 0, 2 ) as $item ) { $term = $this->normalize_compare_phrase( (string) $item ); if ( '' === $term ) { continue; } $args = [ 'limit' => 1, 'status' => 'publish', 'return' => 'objects', 's' => $term, ]; if ( null !== $filters['max_price'] ) { $args['max_price'] = (string) $filters['max_price']; } if ( null !== $filters['min_price'] ) { $args['min_price'] = (string) $filters['min_price']; } if ( $filters['in_stock_only'] ) { $args['stock_status'] = 'instock'; } $matches = wc_get_products( $args ); if ( empty( $matches ) || ! $matches[0] instanceof WC_Product ) { continue; } $product = $matches[0]; $product_id = $product->get_id(); if ( isset( $products[ $product_id ] ) ) { continue; } $products[ $product_id ] = $product; } return array_values( $products ); } private function looks_like_browse_products_request( $message_lower ) { return (bool) preg_match( '/\b(show|list|browse|view|see)\b.*\b(product|products|shop|catalog|catalogue)\b|\b(all|shop)\s+products?\b/i', $message_lower ); } private function apply_filters( array $products, array $filters ) { $filtered = []; foreach ( $products as $product ) { if ( ! $product instanceof WC_Product ) { continue; } if ( $filters['in_stock_only'] && ! $product->is_in_stock() ) { continue; } $price = (float) $product->get_price(); if ( null !== $filters['min_price'] && $price < (float) $filters['min_price'] ) { continue; } if ( null !== $filters['max_price'] && $price > (float) $filters['max_price'] ) { continue; } $filtered[] = $product; } return $filtered; } private function fallback_price_candidates( array $filters ) { $candidates = wc_get_products( [ 'limit' => 120, 'status' => 'publish', 'return' => 'objects', 'orderby' => 'date', 'order' => 'DESC', ] ); return $this->apply_filters( $candidates, $filters ); } private function filter_by_term( array $products, $taxonomy, $term_slug ) { $term_slug = strtolower( trim( (string) $term_slug ) ); if ( $term_slug === '' ) { return $products; } $filtered = []; foreach ( $products as $product ) { if ( ! $product instanceof WC_Product ) { continue; } $terms = get_the_terms( $product->get_id(), $taxonomy ); if ( ! is_array( $terms ) ) { continue; } foreach ( $terms as $term ) { if ( ! $term instanceof WP_Term ) { continue; } if ( strtolower( (string) $term->slug ) === $term_slug ) { $filtered[] = $product; break; } } } return $filtered; } private function rank_products( array $products, array $filters, $message_lower ) { $search = $filters['search'] !== '' ? strtolower( $filters['search'] ) : ''; if ( empty( $search ) && empty( $filters['category'] ) && empty( $filters['tag'] ) ) { if ( 'lowest_price' === $filters['intent'] ) { usort( $products, static function( $left, $right ) { $left_price = $left instanceof WC_Product ? (float) $left->get_price() : 0.0; $right_price = $right instanceof WC_Product ? (float) $right->get_price() : 0.0; if ( $left_price === $right_price ) { return 0; } return $left_price <=> $right_price; } ); return $products; } if ( 'most_expensive' === $filters['intent'] ) { usort( $products, static function( $left, $right ) { $left_price = $left instanceof WC_Product ? (float) $left->get_price() : 0.0; $right_price = $right instanceof WC_Product ? (float) $right->get_price() : 0.0; if ( $left_price === $right_price ) { return 0; } return $right_price <=> $left_price; } ); return $products; } } $scored = []; // Computed once rather than per product. $search_slug = $search !== '' ? sanitize_title( $search ) : ''; $search_terms = $search !== '' ? $this->prepare_search_terms( $search ) : []; foreach ( $products as $product ) { if ( ! $product instanceof WC_Product ) { continue; } $score = 0; $name = strtolower( (string) $product->get_name() ); $slug = strtolower( (string) $product->get_slug() ); if ( $search !== '' ) { if ( $name === $search || $slug === $search_slug ) { $score += 250; } elseif ( $this->contains_whole_word( $name, $search ) || $this->contains_whole_word( $slug, $search_slug ) ) { $score += 180; } foreach ( $search_terms as [ $term, $term_slug ] ) { if ( $this->contains_whole_word( $name, $term ) || $this->contains_whole_word( $slug, $term_slug ) ) { $score += 40; } } } if ( $filters['search'] !== '' ) { if ( $name === $search ) { $score += 120; } elseif ( strpos( $name, $search ) !== false ) { $score += 70; } } if ( $filters['category'] !== '' ) { $score += 30; } if ( $filters['tag'] !== '' ) { $score += 20; } if ( $filters['in_stock_only'] && $product->is_in_stock() ) { $score += 25; } if ( null !== $filters['min_price'] || null !== $filters['max_price'] ) { $price = (float) $product->get_price(); $midpoint = 0.0; if ( null !== $filters['min_price'] && null !== $filters['max_price'] ) { $midpoint = ( (float) $filters['min_price'] + (float) $filters['max_price'] ) / 2; } elseif ( null !== $filters['min_price'] ) { $midpoint = (float) $filters['min_price']; } else { $midpoint = (float) $filters['max_price']; } $distance = abs( $price - $midpoint ); $score += max( 0, 20 - (int) round( $distance / 10 ) ); } if ( strpos( $message_lower, $name ) !== false ) { $score += 40; } $scored[] = [ 'product' => $product, 'score' => $score, ]; } usort( $scored, static function( $a, $b ) { return (int) $b['score'] <=> (int) $a['score']; } ); $sorted = []; foreach ( $scored as $entry ) { $sorted[] = $entry['product']; } return $sorted; } private function find_strict_search_matches( $search, array $filters ) { $search = strtolower( trim( (string) $search ) ); if ( $search === '' ) { return []; } $search_slug = sanitize_title( $search ); $search_terms = $this->prepare_search_terms( $search ); // Match against post title/slug only (one lightweight query), then build // WC_Product objects for the matches instead of for all 120 candidates. $candidates = get_posts( [ 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 120, 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, 'suppress_filters' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, // Same product-type restriction wc_get_products() applies by default. 'tax_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query [ 'taxonomy' => 'product_type', 'field' => 'slug', 'terms' => array_keys( wc_get_product_types() ), ], ], ] ); $exact_ids = []; $whole_word_ids = []; foreach ( $candidates as $post ) { if ( ! $post instanceof WP_Post ) { continue; } $name = strtolower( (string) $post->post_title ); $slug = strtolower( (string) $post->post_name ); if ( $name === $search || $slug === $search_slug ) { $exact_ids[] = (int) $post->ID; continue; } if ( $this->matches_all_terms( $name, $slug, $search_terms ) ) { $whole_word_ids[] = (int) $post->ID; } } $exact = $this->load_products( $exact_ids, $filters['in_stock_only'] ); return ! empty( $exact ) ? $exact : $this->load_products( $whole_word_ids, $filters['in_stock_only'] ); } private function load_products( array $ids, $in_stock_only ) { if ( empty( $ids ) ) { return []; } update_meta_cache( 'post', $ids ); update_object_term_cache( $ids, 'product' ); $products = []; foreach ( $ids as $id ) { $product = wc_get_product( $id ); if ( ! $product instanceof WC_Product ) { continue; } if ( $in_stock_only && ! $product->is_in_stock() ) { continue; } $products[] = $product; } return $products; } /** * Lower-cased search terms paired with their slug form, computed once per search. * * @return array */ private function prepare_search_terms( $search ) { $prepared = []; foreach ( preg_split( '/\s+/', strtolower( trim( (string) $search ) ), -1, PREG_SPLIT_NO_EMPTY ) as $term ) { $term = strtolower( sanitize_text_field( $term ) ); if ( $term !== '' ) { $prepared[] = [ $term, sanitize_title( $term ) ]; } } return $prepared; } private function matches_all_terms( $name, $slug, array $prepared_terms ) { foreach ( $prepared_terms as [ $term, $term_slug ] ) { if ( ! $this->contains_whole_word( $name, $term ) && ! $this->contains_whole_word( $slug, $term_slug ) ) { return false; } } return true; } private function prefer_exact_name_matches( array $products, $search ) { $search = strtolower( trim( (string) $search ) ); if ( $search === '' ) { return $products; } $exact = []; $whole_word = []; $search_slug = sanitize_title( $search ); foreach ( $products as $product ) { if ( ! $product instanceof WC_Product ) { continue; } $name = strtolower( (string) $product->get_name() ); $slug = strtolower( (string) $product->get_slug() ); if ( $name === $search || $slug === $search_slug ) { $exact[] = $product; continue; } if ( $this->contains_whole_word( $name, $search ) || $this->contains_whole_word( $slug, $search_slug ) ) { $whole_word[] = $product; } } return ! empty( $exact ) ? $exact : ( ! empty( $whole_word ) ? $whole_word : $products ); } private function filter_search_results_to_query( array $products, $search ) { $search = strtolower( trim( (string) $search ) ); if ( $search === '' ) { return $products; } $search_slug = sanitize_title( $search ); $search_terms = $this->prepare_search_terms( $search ); $exact = []; $whole_word = []; foreach ( $products as $product ) { if ( ! $product instanceof WC_Product ) { continue; } $name = strtolower( (string) $product->get_name() ); $slug = strtolower( (string) $product->get_slug() ); if ( $name === $search || $slug === $search_slug ) { $exact[] = $product; continue; } if ( $this->matches_all_terms( $name, $slug, $search_terms ) ) { $whole_word[] = $product; } } return ! empty( $exact ) ? $exact : ( ! empty( $whole_word ) ? $whole_word : $products ); } private function contains_whole_word( $haystack, $needle ) { $haystack = strtolower( (string) $haystack ); $needle = strtolower( trim( (string) $needle ) ); if ( $haystack === '' || $needle === '' ) { return false; } return (bool) preg_match( '/(^|[^a-z0-9])' . preg_quote( $needle, '/' ) . '([^a-z0-9]|$)/i', $haystack ); } private function resolve_show_more_url( array $filters, $message_lower ) { if ( $filters['category'] !== '' ) { $term = get_term_by( 'slug', $filters['category'], 'product_cat' ); if ( $term instanceof WP_Term ) { $url = get_term_link( $term ); if ( ! is_wp_error( $url ) ) { return esc_url_raw( $url ); } } } if ( $filters['tag'] !== '' ) { $term = get_term_by( 'slug', $filters['tag'], 'product_tag' ); if ( $term instanceof WP_Term ) { $url = get_term_link( $term ); if ( ! is_wp_error( $url ) ) { return esc_url_raw( $url ); } } } // Last attempt: infer a matching category/tag directly from message. $cat_url = $this->resolve_matching_term_url( 'product_cat', $message_lower ); if ( $cat_url !== '' ) { return $cat_url; } $tag_url = $this->resolve_matching_term_url( 'product_tag', $message_lower ); if ( $tag_url !== '' ) { return $tag_url; } $shop_url = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'shop' ) : ''; return $shop_url ? esc_url_raw( $shop_url ) : esc_url_raw( home_url( '/' ) ); } private function resolve_matching_term_url( $taxonomy, $message_lower ) { $term = $this->find_term_in_message( $taxonomy, $message_lower ); if ( ! $term instanceof WP_Term ) { return ''; } $url = get_term_link( $term ); return is_wp_error( $url ) ? '' : esc_url_raw( $url ); } private function extract_matching_term_slug( $taxonomy, $message_lower ) { $term = $this->find_term_in_message( $taxonomy, $message_lower ); return $term instanceof WP_Term ? sanitize_title( $term->slug ) : ''; } /** * First term of $taxonomy whose name or slug appears in the message. * Term lists are fetched once per taxonomy per request. */ private function find_term_in_message( $taxonomy, $message_lower ) { if ( ! isset( $this->terms_cache[ $taxonomy ] ) ) { $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => true, 'number' => 80, ] ); $this->terms_cache[ $taxonomy ] = ( is_wp_error( $terms ) || ! is_array( $terms ) ) ? [] : $terms; } foreach ( $this->terms_cache[ $taxonomy ] as $term ) { if ( ! $term instanceof WP_Term ) { continue; } $name = strtolower( trim( (string) $term->name ) ); $slug = strtolower( trim( (string) $term->slug ) ); if ( ( $name !== '' && strpos( $message_lower, $name ) !== false ) || ( $slug !== '' && strpos( $message_lower, $slug ) !== false ) ) { return $term; } } return null; } private function extract_search_keywords( $message_lower ) { $stop_words = [ 'can', 'you', 'i', 'me', 'my', 'we', 'he', 'she', 'it', 'they', 'them', 'is', 'are', 'am', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'from', 'with', 'by', 'suggest', 'show', 'find', 'give', 'get', 'recommend', 'help', 'want', 'need', 'please', 'thanks', 'thank', 'hi', 'hello', 'hey', 'ok', 'okay', 'price', 'pricing', 'cost', 'under', 'below', 'budget', 'product', 'products', 'site', 'shop', 'catalog', 'catalogue', 'lowest', 'cheapest', 'most', 'reviewed', 'reviews', 'review', 'rated', 'over', 'above', 'greater', 'greaterthan', 'more', 'than', 'higher', 'rupee', 'rupees', 'inr', 'rs', ]; $words = preg_split( '/[\s\-_,\.!?]+/', trim( $message_lower ), -1, PREG_SPLIT_NO_EMPTY ); $keywords = []; foreach ( $words as $word ) { $word = trim( $word ); if ( $word === '' ) { continue; } if ( preg_match( '/^[0-9]+(?:\.[0-9]+)?$/', $word ) ) { continue; } if ( strlen( $word ) > 2 && ! in_array( $word, $stop_words, true ) ) { $keywords[] = $word; } } return ! empty( $keywords ) ? implode( ' ', array_slice( $keywords, 0, 3 ) ) : ''; } private function extract_max_price( $message ) { $patterns = [ '/(?:under|below|less\s+than|upto|up\s*to)\s*(?:rs\.?|rupees?|inr|₹|\$|₪)?\s*([0-9]+(?:\.[0-9]+)?)/i', '/(?:rs\.?|rupees?|inr|₹|\$|₪)\s*([0-9]+(?:\.[0-9]+)?)/i', '/([0-9]+(?:\.[0-9]+)?)\s*(?:rs\.?|rupees?|inr|₹|\$|₪)/i', ]; foreach ( $patterns as $pattern ) { if ( preg_match( $pattern, $message, $matches ) ) { return (float) $matches[1]; } } return null; } private function extract_min_price( $message ) { $patterns = [ '/(?:over|above|greater\s*than|more\s*than|higher\s*than)\s*(?:rs\.?|rupees?|inr|₹|\$|₪)?\s*([0-9]+(?:\.[0-9]+)?)/i', '/(?:rs\.?|rupees?|inr|₹|\$|₪)\s*([0-9]+(?:\.[0-9]+)?)\s*(?:and\s*above|\+\s*$)/i', ]; foreach ( $patterns as $pattern ) { if ( preg_match( $pattern, $message, $matches ) ) { return (float) $matches[1]; } } return null; } private function build_applied_filters_payload( array $filters, array $compare_items = [] ) { return [ 'intent' => (string) $filters['intent'], 'search' => (string) $filters['search'], 'min_price' => null !== $filters['min_price'] ? (float) $filters['min_price'] : null, 'max_price' => null !== $filters['max_price'] ? (float) $filters['max_price'] : null, 'in_stock_only' => (bool) $filters['in_stock_only'], 'category' => (string) $filters['category'], 'tag' => (string) $filters['tag'], 'sort_by' => (string) $filters['sort_by'], 'compare_items' => array_values( array_slice( $compare_items, 0, 2 ) ), ]; } private function get_max_products_to_show( $is_browse_request = false ) { $max_products = absint( get_option( 'acosmaia_max_products', 3 ) ); return max( 1, min( 20, $max_products > 0 ? $max_products : 3 ) ); } }