Support » Plugin: WooCommerce » Adding a search/sort parameter for quantity

  • I was shuttled over here by the devs at GIT. Hopefully someone here will be able to help me out.

    Perhaps my attempt was overly simplistic. Suffice to say, it didn’t work:

    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
        $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
        if ( 'by_quantity' == $orderby_value ) {
            $args['orderby'] = 'meta_value';
            $args['order'] = 'desc';
            $args['meta_key'] = 'stock';
        return $args;

    What I’d rather do is filter class-wc-settings-products.php and add the ability to do a default sort by quantity in the settings, since that’s really what they want. If I can add it to the base code, then I can make it a default directly through the WP Settings.

    Could one of you help me out with that filter?


Viewing 1 replies (of 1 total)
  • Coen was right, github issues isn’t support 🙂

    Your code sample is missing the filter that ads ‘by_quantity’ to the sort by list. Refer to the gist here:

    And make sure you have this code:

    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    function custom_woocommerce_catalog_orderby( $sortby ) {
    	$sortby['by_quantity'] = 'Random';
    	return $sortby;
Viewing 1 replies (of 1 total)
  • The topic ‘Adding a search/sort parameter for quantity’ is closed to new replies.