Add WooCommerce product sorting and price filtering without a plugin

Custom product sorting and a price filter in WooCommerce without a plugin

The “Sort by” dropdown of a WooCommerce shop offers popularity, rating, date and price. A shop of books or wines may need “Name A–Z”, and a simple “under 50 / 50–100 / over 100” price filter is often enough without a filter plugin. Both are a few filters on WooCommerce’s own catalogue query.

Problem: The shop needs “Name A–Z” and “Name Z–A” in the sorting dropdown and a price range filter in the sidebar, without installing a product filter plugin.

Solution: Add sorting options with woocommerce_catalog_orderby, translate them into query arguments with woocommerce_get_catalog_ordering_args, and filter the price range in woocommerce_product_query with a meta_query on _price from min_price/max_price URL parameters.

Sorting by name

<?php
add_filter( 'woocommerce_catalog_orderby', function ( $options ) {
    $options['title-asc']  = __( 'Name: A to Z', 'textdomain' );
    $options['title-desc'] = __( 'Name: Z to A', 'textdomain' );
    return $options;
} );

add_filter( 'woocommerce_get_catalog_ordering_args', function ( $args, $orderby, $order ) {
    if ( 'title-asc' === $orderby ) {
        $args['orderby'] = 'title';
        $args['order']   = 'ASC';
    } elseif ( 'title-desc' === $orderby ) {
        $args['orderby'] = 'title';
        $args['order']   = 'DESC';
    }
    return $args;
}, 10, 3 );

The key of the option (title-asc) becomes the ?orderby= parameter of the URL, and WooCommerce passes it to the second filter. To make it the default, set it in Customize → WooCommerce → Product Catalog or with the woocommerce_default_catalog_orderby filter.

A price range filter

WooCommerce already understands ?min_price= and ?max_price= in shop URLs (its price filter widget uses them), using its product lookup table. A simple set of links is enough:

<?php
function ha_price_filter_links() {
    $ranges = array(
        array( 'label' => __( 'Under 50', 'textdomain' ), 'min' => 0, 'max' => 50 ),
        array( 'label' => __( '50 to 100', 'textdomain' ), 'min' => 50, 'max' => 100 ),
        array( 'label' => __( 'Over 100', 'textdomain' ), 'min' => 100, 'max' => '' ),
    );

    echo '<ul class="ha-price-filter">';
    foreach ( $ranges as $range ) {
        $url = remove_query_arg( array( 'min_price', 'max_price', 'paged' ) );
        $url = add_query_arg( array_filter( array( 'min_price' => $range['min'], 'max_price' => $range['max'] ), 'strlen' ), $url );
        printf( '<li><a href="%s">%s</a></li>', esc_url( $url ), esc_html( $range['label'] ) );
    }
    printf( '<li><a href="%s">%s</a></li>', esc_url( remove_query_arg( array( 'min_price', 'max_price', 'paged' ) ) ), esc_html__( 'All prices', 'textdomain' ) );
    echo '</ul>';
}

Print it in the shop sidebar or above the product loop (add_action( 'woocommerce_before_shop_loop', 'ha_price_filter_links', 25 )). The page parameter is removed so a filter never lands on an empty page 5.

The links keep the current sorting and other parameters, because they start from the current URL.

A custom filter on the query

For conditions WooCommerce does not know, change the product query itself:

<?php
add_action( 'woocommerce_product_query', function ( $query ) {
    if ( ! empty( $_GET['in_stock'] ) ) {
        $meta   = (array) $query->get( 'meta_query' );
        $meta[] = array( 'key' => '_stock_status', 'value' => 'instock' );
        $query->set( 'meta_query', $meta );
    }
} );

woocommerce_product_query runs only for the main shop and product archive queries, so it does not affect widgets or related products.

NOTE: Sorting by title sorts by the stored string, so “Item 10” comes before “Item 2”, and accented letters follow the database collation. For a shop where this matters, store a separate sort key in a custom field and sort by it with meta_value.