Include Actual Lowest & Highest Prices in Shop Page Sorting Options

These code snippets enable you to include the actual product prices for lowest and highest prices in your ship page sotting options.

Rather than display Sort by Price : Highest to Lowest or Lowest to Highest, you can display the actual lowest and highest prices so the drop down options include :

Sort by Price : 10 – 300 where 10 is the lowest price of any product and 300 is the highest.

add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_sorting_options_v1' );
function custom_woocommerce_sorting_options_v1( $options ) {
    // Get the minimum and maximum prices
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'fields' => 'ids',
    );

    $products = new WP_Query( $args );

    if ( ! empty( $products->posts ) ) {
        $min_price = PHP_INT_MAX;
        $max_price = PHP_INT_MIN;

        foreach ( $products->posts as $product_id ) {
            $product = wc_get_product( $product_id );
            $price = $product->get_price();

            if ( $price ) {
                if ( $price < $min_price ) {
                    $min_price = $price;
                }
                if ( $price > $max_price ) {
                    $max_price = $price;
                }
            }
        }

        // Update sorting labels with price range
          $options['price'] = sprintf( 'Sort by price: %d - %d', $min_price, $max_price );
      //  $options['price_desc'] = sprintf( 'Sort by price: %d - %d', $min_price, $max_price );
    }

    return $options;
}

And here’s the output on the frontend.

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.