=> $price_range, ); } /** * Add the `posts_clauses` filter to the main query. * * @param array $clauses The query clauses. * @param WP_Query $query The WP_Query instance. */ public function add_price_range_filter_posts_clauses( $clauses, $query ) { $query_vars = $query->query_vars; $is_product_collection_block = $query_vars['isProductCollection'] ?? false; if ( ! $is_product_collection_block ) { return $clauses; } $price_range = $query_vars['priceRange'] ?? null; if ( empty( $price_range ) ) { return $clauses; } global $wpdb; $adjust_for_taxes = $this->should_adjust_price_range_for_taxes(); $clauses['join'] = $this->append_product_sorting_table_join( $clauses['join'] ); $min_price = $price_range['min'] ?? null; if ( $min_price ) { if ( $adjust_for_taxes ) { $clauses['where'] .= $this->get_price_filter_query_for_displayed_taxes( $min_price, 'max_price', '>=' ); } else { $clauses['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.max_price >= %f ', $min_price ); } } $max_price = $price_range['max'] ?? null; if ( $max_price ) { if ( $adjust_for_taxes ) { $clauses['where'] .= $this->get_price_filter_query_for_displayed_taxes( $max_price, 'min_price', '<=' ); } else { $clauses['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.min_price <= %f ', $max_price ); } } return $clauses; } /** * Get query for price filters when dealing with displayed taxes. * * @param float $price_filter Price filter to apply. * @param string $column Price being filtered (min or max). * @param string $operator Comparison operator for column. * @return string Constructed query. */ protected function get_price_filter_query_for_displayed_taxes( $price_filter, $column = 'min_price', $operator = '>=' ) { global $wpdb; // Select only used tax classes to avoid unwanted calculations. $product_tax_classes = $wpdb->get_col( "SELECT DISTINCT tax_class FROM {$wpdb->wc_product_meta_lookup};" ); if ( empty( $product_tax_classes ) ) { return ''; } $or_queries = array(); // We need to adjust the filter for each possible tax class and combine the queries into one. foreach ( $product_tax_classes as $tax_class ) { $adjusted_price_filter = $this->adjust_price_filter_for_tax_class( $price_filter, $tax_class ); $or_queries[] = $wpdb->prepare( '( wc_product_meta_lookup.tax_class = %s AND wc_product_meta_lookup.`' . esc_sql( $column ) . '` ' . esc_sql( $operator ) . ' %f )', $tax_class, $adjusted_price_filter ); } // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared return $wpdb->prepare( ' AND ( wc_product_meta_lookup.tax_status = "taxable" AND ( 0=1 OR ' . implode( ' OR ', $or_queries ) . ') OR ( wc_product_meta_lookup.tax_status != "taxable" AND wc_product_meta_lookup.`' . esc_sql( $column ) . '` ' . esc_sql( $operator ) . ' %f ) ) ', $price_filter ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared } /** * Adjusts a price filter based on a tax class and whether or not the amount includes or excludes taxes. * * This calculation logic is based on `wc_get_price_excluding_tax` and `wc_get_price_including_tax` in core. * * @param float $price_filter Price filter amount as entered. * @param string $tax_class Tax class for adjustment. * @return float */ protected function adjust_price_filter_for_tax_class( $price_filter, $tax_class ) { $tax_display = get_option( 'woocommerce_tax_display_shop' ); $tax_rates = WC_Tax::get_rates( $tax_class ); $base_tax_rates = WC_Tax::get_base_tax_rates( $tax_class ); // If prices are shown incl. tax, we want to remove the taxes from the filter amount to match prices stored excl. tax. if ( 'incl' === $tax_display ) { /** * Filters if taxes should be removed from locations outside the store base location. * * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing * with out of base locations. e.g. If a product costs 10 including tax, all users will pay 10 * regardless of location and taxes. * * @since 2.6.0 * * @internal Matches filter name in WooCommerce core. * * @param boolean $adjust_non_base_location_prices True by default. * @return boolean */ $taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $price_filter, $base_tax_rates, true ) : WC_Tax::calc_tax( $price_filter, $tax_rates, true ); return $price_filter - array_sum( $taxes ); } // If prices are shown excl. tax, add taxes to match the prices stored in the DB. $taxes = WC_Tax::calc_tax( $price_filter, $tax_rates, false ); return $price_filter + array_sum( $taxes ); } /** * Determines if price filters need adjustment based on the tax display settings. * * This function checks if there's a discrepancy between how prices are stored in the database * and how they are displayed to the user, specifically with respect to tax inclusion or exclusion. * It returns true if an adjustment is needed, indicating that the price filters should account for this * discrepancy to display accurate prices. * * @return bool True if the price filters need to be adjusted for tax display settings, false otherwise. */ private function should_adjust_price_range_for_taxes() { $display_setting = get_option( 'woocommerce_tax_display_shop' ); // Tax display setting ('incl' or 'excl'). $price_storage_method = wc_prices_include_tax() ? 'incl' : 'excl'; return $display_setting !== $price_storage_method; } /** * Generates a post__in query to filter products to the set of provided IDs. * * @param int[]|false $handpicked_products The products to filter. * * @return array The post__in query. */ private function get_handpicked_query( $handpicked_products ) { if ( false === $handpicked_products ) { return array(); } return array( 'post__in' => $handpicked_products, ); } /** * Return a query for on sale products. * * @param bool $is_on_sale Whether to query for on sale products. * * @return array */ private function get_on_sale_products_query( $is_on_sale ) { if ( ! $is_on_sale ) { return array(); } return array( 'post__in' => wc_get_product_ids_on_sale(), ); } /** * Merge in the first parameter the keys "post_in", "meta_query" and "tax_query" of the second parameter. * * @param array[] ...$queries Query arrays to be merged. * @return array */ private function merge_queries( ...$queries ) { // Rather than a simple merge, some query vars should be held aside and merged differently. $special_query_vars = array( 'post__in' => array(), ); $special_query_keys = array_keys( $special_query_vars ); $merged_query = array_reduce( $queries, function ( $acc, $query ) use ( $special_query_keys, &$special_query_vars ) { if ( ! is_array( $query ) ) { return $acc; } // When the $query has keys but doesn't contain any valid query keys, we unpack/spread it then merge. if ( ! empty( $query ) && empty( array_intersect( $this->get_valid_query_vars(), array_keys( $query ) ) ) ) { return $this->merge_queries( $acc, ...array_values( $query ) ); } // Pull out the special query vars so we can merge them separately. foreach ( $special_query_keys as $query_var ) { if ( isset( $query[ $query_var ] ) ) { $special_query_vars[ $query_var ][] = $query[ $query_var ]; unset( $query[ $query_var ] ); } } return $this->array_merge_recursive_replace_non_array_properties( $acc, $query ); }, array() ); // Perform any necessary special merges. $merged_query['post__in'] = $this->merge_post__in( ...$special_query_vars['post__in'] ); return $merged_query; } /** * Return query params to support custom sort values * * @param string $orderby Sort order option. * * @return array */ private function get_custom_orderby_query( $orderby ) { if ( ! in_array( $orderby, $this->custom_order_opts, true ) || 'post__in' === $orderby ) { return array( 'orderby' => $orderby ); } if ( 'price' === $orderby ) { add_filter( 'posts_clauses', array( $this, 'add_price_sorting_posts_clauses' ), 10, 2 ); return array( 'isProductCollection' => true, 'orderby' => $orderby, ); } // The popularity orderby value here is for backwards compatibility as we have since removed the filter option. if ( 'sales' === $orderby || 'popularity' === $orderby ) { add_filter( 'posts_clauses', array( $this, 'add_sales_sorting_posts_clauses' ), 10, 2 ); return array( 'isProductCollection' => true, 'orderby' => $orderby, ); } if ( 'menu_order' === $orderby ) { return array( 'orderby' => 'menu_order', 'order' => 'ASC', ); } if ( 'random' === $orderby ) { return array( 'orderby' => 'rand', ); } $meta_keys = array( 'rating' => '_wc_average_rating', ); return array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_key' => $meta_keys[ $orderby ], 'orderby' => 'meta_value_num', ); } /** * Add the `posts_clauses` filter to add price-based sorting * * @param array $clauses The list of clauses for the query. * @param WP_Query $query The WP_Query instance. * @return array Modified list of clauses. */ public function add_price_sorting_posts_clauses( $clauses, $query ) { $query_vars = $query->query_vars; $is_product_collection_block = $query_vars['isProductCollection'] ?? false; if ( ! $is_product_collection_block ) { return $clauses; } $orderby = $query_vars['orderby'] ?? null; if ( 'price' !== $orderby ) { return $clauses; } $clauses['join'] = $this->append_product_sorting_table_join( $clauses['join'] ); $is_ascending_order = 'asc' === strtolower( $query_vars['order'] ?? 'desc' ); $clauses['orderby'] = $is_ascending_order ? 'wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC' : 'wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC'; return $clauses; } /** * Add the `posts_clauses` filter to add sales-based sorting * * @param array $clauses The list of clauses for the query. * @param WP_Query $query The WP_Query instance. * @return array Modified list of clauses. */ public function add_sales_sorting_posts_clauses( $clauses, $query ) { $query_vars = $query->query_vars; $is_product_collection_block = $query_vars['isProductCollection'] ?? false; if ( ! $is_product_collection_block ) { return $clauses; } $orderby = $query_vars['orderby'] ?? null; // The popularity orderby value here is for backwards compatibility as we have since removed the filter option. if ( 'sales' !== $orderby && 'popularity' !== $orderby ) { return $clauses; } $clauses['join'] = $this->append_product_sorting_table_join( $clauses['join'] ); $is_ascending_order = 'asc' === strtolower( $query_vars['order'] ?? 'desc' ); $clauses['orderby'] = $is_ascending_order ? 'wc_product_meta_lookup.total_sales ASC, wc_product_meta_lookup.product_id ASC' : 'wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC'; return $clauses; } /** * Join wc_product_meta_lookup to posts if not already joined. * * @param string $sql SQL join. * @return string */ protected 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; } /** * Merge all of the 'post__in' values and return an array containing only values that are present in all arrays. * * @param int[][] ...$post__in The 'post__in' values to be merged. * * @return int[] The merged 'post__in' values. */ private function merge_post__in( ...$post__in ) { if ( empty( $post__in ) ) { return array(); } // Since we're using array_intersect, any array that is empty will result // in an empty output array. To avoid this we need to make sure every // argument is a non-empty array. $post__in = array_filter( $post__in, function ( $val ) { return is_array( $val ) && ! empty( $val ); } ); if ( empty( $post__in ) ) { return array(); } // Since the 'post__in' filter is exclusionary we need to use an intersection of // all of the arrays. This ensures one query doesn't add options that another // has otherwise excluded from the results. if ( count( $post__in ) > 1 ) { $post__in = array_intersect( ...$post__in ); // An empty array means that there was no overlap between the filters and so // the query should return no results. if ( empty( $post__in ) ) { return array( -1 ); } } else { $post__in = reset( $post__in ); } return array_values( array_unique( $post__in, SORT_NUMERIC ) ); } }