Looking to sort WooCommerce products in ascending order of price by default?

This can be done in two ways.

Method 1

Use the products shortcode and specify the orderby attribute’s value as price.

[products orderby="price"]

Method 2

If your theme/plugin is already using the [products] shortcode like the Products List component of Oxygen, woocommerce_shortcode_products_query filter can be used to modify the query.

Add the following code snippet:

add_filter( 'woocommerce_shortcode_products_query', 'woocommerce_shortcode_products_orderby' );
function woocommerce_shortcode_products_orderby( $args ) {
    $standard_array = array( 'menu_order', 'title', 'date', 'rand', 'id' );

    if ( isset( $args['orderby'] ) && ! in_array( $args['orderby'], $standard_array ) ) {
        $args['meta_key'] = '_price';
        $args['orderby']  = 'meta_value_num'; 
    }

    return $args;
}

Note: The above applies to all instances of the [products] shortcode.

Reference

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE

Comments

You need to be logged in to view and post comments.
Not a member yet? Join here.
magnifiercross
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: